天梯赛 L2 题目汇总

L2-001 城市间紧急救援

#include <bits/stdc++.h>
using namespace std;
#define LL __int128
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
const ll lmax=2e18;
const ll mod =1e9+7;
const ll N=1e6+10;
const ll M=2e5 + 10;
typedef pair<int,int> PII;
ll head[N],cnt,num[N],pre[N],dp1[N],dp2[N];
ll n,m,s,d,dis[N];
bool vis[N];
struct EDGE
{
	ll x,y,w,neaxty;
}edge[N];
struct NODE
{
	ll num,w;
	bool operator<(const NODE&x)const
	{
		return w>x.w;
	}
};
void addedge(int x,int y,int w)
{
	edge[++cnt].x = x;
	edge[cnt].y = y;
	edge[cnt].w = w;
	edge[cnt].neaxty = head[x];
	head[x] = cnt;
}
void dj()
{
	memset(dis,0x3f,sizeof dis);
	priority_queue<NODE>q;
	q.push({s,0});
	dis[s]=0;
	dp1[s]=1;
	dp2[s]=num[s];
	while(q.size())
	{
		int x= q.top().num;
		q.pop();
		if(vis[x])
			continue;
		vis[x]=1;
		for(int i=head[x];i;i=edge[i].neaxty)
		{
			int y = edge[i].y;
			int tem = dp2[x] + num[y];
			if(dis[y]>dis[x]+edge[i].w)
			{
				dis[y] = dis[x] + edge[i].w;
				pre[y] = x;
				dp1[y] = dp1[x];
				dp2[y] = dp2[x] + num[y];
				q.push({y,dis[y]});
			}
			else if(dis[y] == dis[x]+edge[i].w)
			{
				dp1[y] = dp1[x]+dp1[y];
				if(tem>dp2[y])
				{
					dp2[y] = tem;
					pre[y] = x;
					
				}
			}
			
		}
	}
}
int main()
{
	cin>>n>>m>>s>>d;
	for(int i=0;i<n;i++)cin>>num[i];
	for(int i=1;i<=m;i++)
	{
		int x,y,w;
		cin>>x>>y>>w;
		addedge(x,y,w);//
		addedge(y,x,w);
	}
	dj();
	ll ans1=dp1[d],ans2=dp2[d],las=d;
	int tmp[600],tmp_cnt=0;
	while(las!=s)
	{
		tmp[++tmp_cnt] = las;
		
		las = pre[las];
		
	}
    tmp[++tmp_cnt] = s;
	//ans2 += num[s];
	cout<<ans1<<" "<<ans2<<endl;
	
	for(int i=tmp_cnt;i>=1;i--)
	{
		if(i!=tmp_cnt)
			cout<<" ";
		cout<<tmp[i];
	}
	return 0;
} 

L2-002 链表去重(22)

#include <bits/stdc++.h>
using namespace std;
#define LL __int128
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
const ll lmax=2e18;
const ll mod =1e9+7;
const ll N=1e5 + 10;
const ll M=2e5 + 10;
typedef pair<int,int> PII;
int n,head;//链表头地址 
int rm_head;//删除的链表头 
bool vis[11111];
vector<int>v;
unordered_map<int,int>dz,las,pre;//对应地址的键值,
int main()
{
	cin>>head>>n;
	
	for(int i=1;i<=n;i++)
	{
		int x,y,z;
		cin>>x>>y>>z;
		dz[x]=y;
		las[x]=z;//维护前驱和后继 
		pre[z]=x;
	}
	int tem_loc = head , l=-1;//记录地址 
	while(tem_loc!=-1)
	{
		int x = abs(dz[tem_loc]);
		if(vis[x])//重复了 
		{
			v.push_back(tem_loc);
			if(l==-1)//开始维护连续的删除 
				l =  pre[tem_loc];
		}
		else if(l!=-1)//删够了 
		{
			las[l] = tem_loc;
			pre[tem_loc] = l; 
			l=-1;//重置 
		}
		vis[x]=1;
		tem_loc = las[tem_loc];
	}
	if(l!=1) las[l] = -1;
	//输出
	tem_loc = head; 
	while(tem_loc!=-1)
	{
		int x = dz[tem_loc];
		if(tem_loc!=head)cout<<endl;
		//cout<<tem_loc<<" "<<x<<" "<<las[tem_loc];
        if(las[tem_loc]!=-1)
		    printf("%05d %d %05d",tem_loc,x,las[tem_loc]);
        else
            printf("%05d %d %d",tem_loc,x,las[tem_loc]);
		tem_loc = las[tem_loc];
	}
	for(int i=0;i<v.size();i++)
	{
		cout<<endl;
		int tem = i+1<v.size()?v[i+1]:-1;
        if(tem!=-1)
		    printf("%05d %d %05d",v[i],dz[v[i]],tem);
		else
            printf("%05d %d %d",v[i],dz[v[i]],tem);
	}
	return 0;
}

L2-004 这是二叉搜索树吗?

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf = 0x3f3f3f3f;
const int maxn = 1010;
int n, flag;
int pr[maxn];

vector<int>v;

void find(int l, int r) {
	if (l > r) return;
	int tl = l + 1, tr = r;
	if (!flag) {
		while (tl <= r && pr[tl] < pr[l]) tl++;//从左往右搜第一个比根节点大的
		while (tr > l && pr[tr] >= pr[l]) tr--;//从右往左搜第一个小于等于根节点
	}
	else {
		while (tl <= r && pr[tl] >= pr[l]) tl++;
		while (tr > l && pr[tr] < pr[l]) tr--;
	}
	if (tl - tr != 1) return;
	//cout<<l+1<<" "<<tr<<endl;
	//cout<<tl<<" "<<r<<endl;
	find(l + 1, tl-1);//左子树
	find(tr+1, r);//右子树
	v.push_back(pr[l]);//根节点
}

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; i++) scanf("%d", &pr[i]);
	find(0, n - 1);
	if (v.size() != n) {
		flag = 1;
		v.clear();
		find(0, n - 1);
	}
	if (v.size() != n) printf("NO\n");
	else {
		printf("YES\n");
		for (int i = 0; i < n; i++) {
			printf("%d%c", v[i], i == (n - 1) ? '\n' : ' ');
		}
	}
	return 0;
}

L2-006 树的遍历

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include<unordered_map>
#include<queue>
using namespace std;
typedef long long ll;
const int N = 45;
int n;
int postorder[N], inorder[N]; //后序遍历,中序遍历
unordered_map<int, int>l, r, pos; //每个点左右儿子,pos代表在中序遍历中每个值对应的下标
int build(int il, int ir, int pl, int pr) { //中序遍历和后序遍历左右端点
	int root = postorder[pr]; //根节点是后序遍历的右端点
	int k = pos[root]; //根节点在中序遍历中的下标
 
	if (il < k) l[root] = build(il, k - 1, pl, pl + k - 1 - il); //左子树存在
	//左子树中序遍历和后序遍历的长度一样所以 x-pl=k-1-il
	if (ir > k) r[root] = build(k + 1, ir, pl + k - 1 - il + 1, pr - 1);//右子树存在
 
	return root;
}
void bfs(int root, int n) {
	queue<int>q;
	q.push(root);
	while (q.size()) {
		auto t = q.front();
		q.pop();
		n--;
		if (n != 0) cout << t << " ";
		else cout << t;
		if (l.count(t)) q.push(l[t]); //左子树存在,插入队列
		if (r.count(t)) q.push(r[t]); //右子树存在,插入队列
	}
}
int main() {
	cin >> n;
	for (int i = 0; i < n; i++) cin >> postorder[i];
	for (int i = 0; i < n; i++) {
		cin >> inorder[i];
		pos[inorder[i]] = i; //中序遍历每个值对应的下标
	}
	int root = build(0, n - 1, 0, n - 1); //中序遍历,后序遍历区间
	bfs(root, n);
	return 0;
}

L2-007 家庭房产

#include <bits/stdc++.h>
using namespace std;
#define LL __int128
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
const ll lmax=2e18;
const ll mod =1e9+7;
const ll N=1e5 + 10;
const ll M=2e5 + 10;
typedef pair<int,int> PII;
int n,fa[N],fc[N],mj[N];
bool vis[N],vis2[N];
int f_ind(int x)
{
	return fa[x]=fa[x]==x?x:f_ind(fa[x]);
}
struct F
{
	int mn_no=0x3f3f3f3f,num;
	double fc,mj;
}fam[N];
vector<F>v;
bool cmp(F x , F y)
{
	if(x.mj/x.num!=y.mj/y.num)
	{
		return x.mj/x.num>y.mj/y.num;
	}
	else
		return x.mn_no<y.mn_no;
}
//一开始自己做的时候,总是想着把fc,mj,num属性,在union的时候累加到父节点,一堆稀奇古怪的错误,最后不了了之
//其实可以到最后才加起来 
int main()
{
	cin>>n;
	for(int i=0;i<N;i++)fa[i]=i;
	for(int i=1;i<=n;i++)
	{
		int x,p1,p2,k;
		cin>>x>>p1>>p2>>k;
		vis[x]=1;
		if(p1!=-1)
		{
			vis[p1]=1;
			fa[f_ind(x)]=f_ind(p1);
		}
		if(p2!=-1)
		{
			vis[p2]=1;
			fa[f_ind(x)]=f_ind(p2);
		}
		for(int j=1;j<=k;j++)
		{
			int y;
			cin>>y;
			vis[y]=1;
			fa[f_ind(x)]=f_ind(y);
		}
		int a,b;
		cin>>a>>b;
		fc[x]+=a;
		mj[x]+=b;
	}
	for(int i=0;i<N;i++)
	{
		if(!vis[i])continue;
		int x = f_ind(i);
		fam[x].mn_no = min(fam[x].mn_no,i);
		fam[x].num++;
		fam[x].fc+=fc[i];
		fam[x].mj+=mj[i];
		vis2[x]=1;
	}	
	for(int i=0;i<N;i++)
	{
		if(vis2[i])
			v.push_back(fam[i]);
	}
	sort(v.begin(),v.end(),cmp);
    cout<<v.size()<<endl;
	for(int i=0;i<v.size();i++)
	{
		if(i!=0)cout<<endl;
		printf("%04d %d %.3f %.3f",v[i].mn_no,v[i].num,v[i].fc/v[i].num,v[i].mj/v[i].num);
	}
	return 0;
}

L2-008 最长对称子串

#include <bits/stdc++.h>
using namespace std;
#define LL __int128
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
const ll lmax=2e18;
const ll mod =1e9+7;
const ll N=1e5 + 10;
const ll M=2e5 + 10;
typedef pair<int,int> PII;
string str;
int ans;
bool judge(int x)
{
	for(int i=0;i+x-1<str.size();i++)
	{
		int l = i , r = i + x - 1;
		while(l<=r&&str[l] == str[r])
		{
			l++,r--;
		}
		if(l>r)
		{
			return 1;
		}
	}
	return 0;
}
int main()
{
	getline(cin,str);
	int l = 1 , r = str.size();
	while(l<=r)
	{
		int mid = (l+r)>>1;
		if(judge(mid)||judge(mid+1))//奇数和偶数一起,因为比如123321,是偶数对称的,如果只judge一个数,(但是)
		{                           //第一次mid可以是偶数y,但后面变到x(奇数)不成功时,就会使得mid变小,但x+1却可以,所以要奇数偶数一起
			l = mid+1;
			ans = max(mid,ans);
		}
		else r = mid-1;
	}
    cout<<ans;
	return 0;
}

L2-011 玩转二叉树

#include <bits/stdc++.h>
using namespace std;
#define LL __int128
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
const ll lmax=2e18;
const ll mod =1e9+7;
const ll N=1e5 + 10;
const ll M=2e5 + 10;
typedef pair<int,int> PII;
int n,a[40],b[40],l[40],r[40],root;
unordered_map<int,int>loc;
int dfs(int ml,int mr,int pl,int pr)
{
	int k = loc[b[pl]];//根节点在中序遍历的位置
	 
	if(k>ml)//左子树存在 
	{
		l[k]=dfs(ml,k-1,pl+1,pl+k-ml); 
	}
	if(k<mr)//右子树存在
	{
		r[k]=dfs(k+1,mr,pl+k-ml+1,pr); 
	}
	return k; 
}
void bfs()
{
	queue<int>q;
	q.push(root);
	bool flag=0;
	while(q.size())
	{
		int x = q.front();q.pop();
		if(flag)cout<<" ";
		cout<<a[x];
		flag=1;
		if(r[x])q.push(r[x]);
		if(l[x])q.push(l[x]);
	}
	return;
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		loc[a[i]]=i;//记录位置 
	}
	for(int i=1;i<=n;i++)cin>>b[i];
	root = dfs(1,n,1,n);
	bfs();
	return 0;
}

L2-013 红色警报

#include <bits/stdc++.h>
using namespace std;
#define LL __int128
#define ll long long
#define ull unsigned long long
const int imax=1e9;
const ll lmax=2e18;
const ll mod =1e9+7;
#define N 600
#define M 5005*2
int n,m,sum,cnt;
int head[N],k,fa[N];
int vis1[N],vis2[N];
struct EDGE
{
	int x,y;
}edge[M];

int f_ind(int x)
{
	return fa[x]=fa[x]==x?x:f_ind(fa[x]);
}
int judge()
{
	int sum=0;
	memset(vis2,0,sizeof vis2);
	for(int i=0;i<n;i++)fa[i]=i;
	
	for(int i=1;i<=cnt;i++)
	{
		int x =edge[i].x;
		int y =edge[i].y;
		if(vis1[x]||vis1[y])continue;
		
		fa[f_ind(x)]=fa[f_ind(y)];
	}
	for(int i=0;i<n;i++)
	{
        if(vis1[i])continue;//该点已经被删掉 
		int x = f_ind(i);
		if(!vis2[x])//这个集合被统计过了
		{
			sum++;
			vis2[x]=1;
		}
	}
	//cout<<sum<<endl;
	return sum;
}
int main()
{
	cin>>n>>m;
	while(m--)
	{
		int x=0,y=0;
		cin>>x>>y;
		edge[++cnt].x=x;
		edge[cnt].y=y;
	}
	int las = 0;
	las = judge();
	cin>>k;
	int tem = n;
	for(int i=1;i<=k;i++)
	{
		int x=0;
		cin>>x;
		vis1[x]=1;
		int tmp = judge();
        //cout<<tmp<<endl;
		if(las<tmp)printf("Red Alert: City %d is lost!\n",x);
		else printf("City %d is lost.\n",x);
		tem--;
		if(tem<=0)cout<<"Game Over."<<endl;
		las = tmp;
	}
	return 0;
} 

L2-014 列车调度

#include <bits/stdc++.h>
using namespace std;
#define LL __int128
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
const ll lmax=2e18;
const ll mod =1e9+7;
const ll N=1e5 + 10;
const ll M=2e5 + 10;
typedef pair<int,int> PII;
int d[N],n,a[N],len=0;
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	
	for(int i=1;i<=n;i++)
	{
		if(!len||d[len]<a[i])
		{
			d[++len]=a[i];
		}
		else
		{
			int x = upper_bound(d+1,d+1+len,a[i])-d;
			d[x]=a[i];
		}
	}
	cout<<len;
	return 0;
}

L2-016 愿天下有情人都是失散多年的兄妹

#include <bits/stdc++.h>
using namespace std;
#define LL __int128
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
const ll lmax=2e18;
const ll mod =1e9+7;
const ll N=1e7 + 10;
const ll M=2e5 + 10;
typedef pair<int,int> PII;
int n,head[N],cnt,k;
char s[N];
struct EDGE
{
	int x,y,neaxty;
}edge[111111];
void addedge(int x,int y)
{
	edge[++cnt].x=x;
	edge[cnt].y=y;
	edge[cnt].neaxty=head[x];
	head[x]=cnt;
}
void bfs(vector<int>&v,int x,int d)
{
	queue<PII>q;
	q.push({x,0});
	while(q.size())
	{
		int nx = q.front().first , step = q.front().second;
		q.pop();
		if(step>4)continue;
		v.push_back(nx);
		for(int i=head[nx];i;i=edge[i].neaxty)
		{
			int ny = edge[i].y;
			q.push({ny,step+1});
		}
	}
	return;
}
bool judge(vector<int>v_x,vector<int>v_y)
{
	for(int i=0;i<v_x.size();i++)
	{
		for(int j=0;j<v_y.size();j++)
		{
			if(v_x[i]==v_y[j])return 1;//乱伦 
		}
	}
	return 0; 
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		int a,c,d;
		char b;
		cin>>a>>b>>c>>d;
		s[a]=b;
		if(c!=-1)
		{
			addedge(a,c);
			s[c]='M';
		}
		if(d!=-1)
		{
			addedge(a,d);
			s[d]='F';
		}
	}
	cin>>k;
	for(int i=1;i<=k;i++)
	{
		int x,y;
		cin>>x>>y;
		if(s[x]==s[y])puts("Never Mind");
		else
		{
			vector<int>v_x,v_y;
			bfs(v_x,x,0);
			bfs(v_y,y,0);
			if(judge(v_x,v_y)) puts("No");
			else puts("Yes");
		}
	}
	return 0;
}

L2-020 功夫传人

#include <bits/stdc++.h>
using namespace std;
#define LL __int128
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
const ll lmax=2e18;
const ll mod =1e9+7;
const ll N=1e5 + 10;
const ll M=2e5 + 10;
typedef pair<int,int> PII;
int head[N],cnt,n;
long double z,r,ans;
long double dedao[N];
struct EDGE
{
	int x,y,neaxty;
}edge[111111];
void addedge(int x,int y)
{
	edge[++cnt].x = x;
	edge[cnt].y=y;
	edge[cnt].neaxty = head[x];
	head[x]=cnt;
}
void bfs()
{
	queue<pair<int,long double>>q;
	q.push({0,z});
	while(q.size())
	{
		int x = q.front().first;
		long double h = q.front().second;
		q.pop();
		
		for(int i=head[x];i;i=edge[i].neaxty)
		{
			int y=edge[i].y;
			if(dedao[y])
			{
				ans+=h*(100-r)/100*dedao[y];
				q.push({y,h*(100-r)/100*dedao[y]});
			}
			else
			{
				q.push({y,h*(100-r)/100});
			}
		}
	}
	return;
}
int main()
{
	cin>>n>>z>>r;
	for(int i=0;i<n;i++)
	{
		int k=0;
		cin>>k;
		if(k==0)
		{
			double h;
			cin>>h;
			dedao[i]=h;
		}
		while(k--)
		{
			int y;
			cin>>y;
			addedge(i,y);
		}
	}
	bfs();
	cout<<(int)ans;
	//printf("%.f",ans);
	return 0;
}

L2-022 重排链表(22)

#include <bits/stdc++.h>
using namespace std;
#define LL __int128
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
const ll lmax=2e18;
const ll mod =1e9+7;
const ll N=1e5 + 10;
const ll M=2e5 + 10;
typedef pair<int,int> PII;
int n,head;
unordered_map<int,int>nex,val;
int cnt;
struct NODE
{
	int adr,val;
}node[N];
int main()
{
	cin>>head>>n;
	for(int i=1;i<=n;i++)
	{
		int a,b,c;
		cin>>a>>b>>c;
		val[a]=b;
		nex[a]=c;
	}
	int las = head;
	while(las!=-1)
	{
		node[++cnt].adr = las;
		node[cnt].val = val[las];
		
		las = nex[las];
	}
	for(int i=cnt;i>(cnt+1)/2;i--)
	{
		int nexaddr;//暂存下一个节点地址 
		int x = 1+cnt-i;
		if(i!=cnt)cout<<endl;
		if(1+cnt-i<i)
			nexaddr = node[x].adr; 
		else 
			nexaddr=-1;
		if(nexaddr!=-1)
			printf("%05d %d %05d",node[i].adr,node[i].val,nexaddr);
		else
			printf("%05d %d %d",node[i].adr,node[i].val,-1);
		if(x<i)
		{
			if(i-1>(cnt+1)/2)
				nexaddr = node[i-1].adr;
			else 
				nexaddr = -1;
			
			cout<<endl;
			if(nexaddr!=-1)
				printf("%05d %d %05d",node[x].adr,node[x].val,nexaddr);
			else
				printf("%05d %d %d",node[x].adr,node[x].val,-1);
		}
	}
	return 0;
}

L2-023 图着色问题 (23)

#include <bits/stdc++.h>
using namespace std;
#define LL __int128
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
const ll lmax=2e18;
const ll mod =1e9+7;
const ll N=50000 + 10;
const ll M=2500000 + 10;
typedef pair<int,int> PII;
int v,e,k,head[N],cnt,n;
int col[N];
bool flag=0;
bool vis[N],vis2[N];
struct EDGE
{
	int x,y,neaxty;
}edge[M];
void addedge(int x,int y)
{
	edge[++cnt].x=x;
	edge[cnt].y=y;
	edge[cnt].neaxty=head[x];
	head[x]=cnt;
}
void dfs(int x,int fa)
{
	if(vis[x])return;
	vis[x]=1;
	for(int i=head[x];i;i=edge[i].neaxty)
	{
		int y = edge[i].y;
		//if(y==fa)continue;
		if(col[x]==col[y])flag=1;
		dfs(y,x);
	}
	return;
}
int main()
{
	cin>>v>>e>>k;
	for(int i=1;i<=e;i++)
	{
		int x,y;
		cin>>x>>y;
		addedge(x,y);
		addedge(y,x);
	}
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		memset(vis,0,sizeof vis);
		memset(vis2,0,sizeof vis2);
		memset(col,0,sizeof col);
		int sum=0;
		for(int j=1;j<=v;j++)
		{
			int x;
			cin>>x;
			if(!vis2[x])
			{
				vis2[x]=1;
				sum++;
			}
			col[j]=x;
		}
		dfs(1,-1);
		if(flag||sum!=k)puts("No");
		else puts("Yes");
		
		flag=0;
	}
	return 0;
}

L2-025 分而治之

#include<bits/stdc++.h>
using namespace std;

int n,m,k,cnt,head[11111];
bool vis[11111];
struct EDGE
{
    int x,y,neaxty;
}edge[11111];
void addedge(int x,int y)
{
    edge[++cnt].x = x;
    edge[cnt].y=y;
    edge[cnt].neaxty = head[x];
    head[x] =cnt;
}
bool judge()
{
    for(int i=1;i<=n;i++)
    {
        if(vis[i])continue;
        for(int j=head[i];j;j=edge[j].neaxty)
        {
            int y = edge[j].y;
            if(vis[y])continue;
            return 0;//能访问到其他点,不是孤立的
        }
    }
    return 1;
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int x,y;
        cin>>x>>y;
        addedge(x,y);
    }
    cin>>k;
    for(int i=1;i<=k;i++)
    {
        memset(vis,0,sizeof vis);
        int h;
        cin>>h;
        for(int j=1;j<=h;j++)
        {
            int x;
            cin>>x;
            vis[x]=1;
        }
        if(judge())puts("YES");
        else puts("NO");
    }
    return 0;
}

L2-026 小字辈

#include<bits/stdc++.h>
using namespace std;
int n;
int head[111111],cnt,mx;
int root;
vector<int>ans;
struct EDGE
{
    int x,y,neaxty;
}edge[211111];
void addedge(int x,int y)
{
    edge[++cnt].x=x;
    edge[cnt].y=y;
    edge[cnt].neaxty = head[x];
    head[x]=cnt;
}
void dfs(int x,int d)
{
    bool flag=0;
    for(int i=head[x];i;i=edge[i].neaxty)
    {
        int y = edge[i].y;
        flag=1;
        dfs(y,d+1);
    }
    
    if(!flag)
    {
        if(mx<d)
            ans.clear();
        if(mx<=d)
        ans.push_back(x);
    }
    mx = max(mx,d);
    return;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        int x;
        cin>>x;
        if(x==-1)
            root=i;
        else
            addedge(x,i);
    }
    dfs(root,1);
    sort(ans.begin(),ans.end());
    cout<<mx<<endl;
    for(int i=0;i<ans.size();i++)
    {
        if(i!=0)cout<<" ";
        cout<<ans[i];
    }
    return 0;
}

L2-029 特立独行的幸福

#include <bits/stdc++.h>
using namespace std;
#define LL __int128
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
const ll lmax=2e18;
const ll mod =1e9+7;
const ll N=1e5 + 10;
const ll M=2e5 + 10;
typedef pair<int,int> PII;
int A,B;
bool vis_as[11111];
int vis_nalone[11111];
int vis_ans[11111];
int vis_tem[11111];
vector<PII>ans;
void as()
{
	vis_as[1]=1;
	vis_as[0]=1;
	for(int i=2;i<=10000/i;i++)
	{
		if(!vis_as[i])
		{
			for(int j=i*i;j<=10000;j+=i)
			{
				vis_as[j]=1;
			}
		}
	}
}
int main()
{
	as();
	cin>>A>>B;
	for(int i=A;i<=B;i++)
	{
		if(vis_nalone[i])continue;//这个点,依附其他幸福数
		vector<int>v;
		int tem=i;
		while(1)
		{
			string str = to_string(tem);
			tem=0;
			for(int j=0;j<str.size();j++)
			{
				int x = (str[j]-'0');
				tem+=x*x;
			}
			
			//cout<<i<<" "<<tem<<endl;
			v.push_back(tem);
			if(tem==1)//幸福数
			{
				//cout<<i<<endl;
				int tmp=v.size();
				if(!vis_as[i])//
					tmp*=2;
				ans.push_back({i,tmp});
				vis_ans[i]=1;
				for(int j=0;j<v.size();j++)
				{
					vis_nalone[v[j]]=1;//不是特立独行的幸福数
					vis_ans[v[j]]=0;//即使在答案中也删掉
				}
				break;
			}
			if(vis_tem[tem]==i)break;//陷入循环
			vis_tem[tem]=i;
		}
	}
	if(ans.size())
	{
		for(int i=0;i<ans.size();i++)
		{
			if(vis_ans[ans[i].first])
			cout<<ans[i].first<<" "<<ans[i].second<<endl;
		}
	}
	else cout<<"SAD";
	return 0;
}

L2-031 深入虎穴

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll N = 1e5+10;
int n;
int head[N],cnt,in[N],root,ans1,ans2;
struct EDGE
{
    int x,y,neaxty;
}edge[11111111];
void addedge(int x,int y)
{
    edge[++cnt].x= x;
    edge[cnt].y=y;
    edge[cnt].neaxty = head[x];
    head[x]=cnt;
}
struct NODE
{
    int num,step;
};
void bfs()
{
    queue<NODE>q;
    q.push({root,0});
    while(q.size())
    {
        int x = q.front().num;
        int step = q.front().step;
        q.pop();
        if(step>ans1)
        {
            ans1 = step;
            ans2 = x;
        }
        for(int i=head[x];i;i=edge[i].neaxty)
        {
            int y =edge[i].y;
            q.push({y,step+1});
        }
    }
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        int k;
        cin>>k;
        for(int j=1;j<=k;j++)
        {
            int y;
            cin>>y;
            addedge(i,y);
            in[y]++;
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(in[i]==0)
        {
            root = i;
            ans2 = i;
            break;
        }
    }
    bfs();
    cout<<ans2;
    return 0;
}

L2-032 彩虹瓶

#include <bits/stdc++.h>
using namespace std;
#define LL __int128
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
const ll lmax=2e18;
const ll mod =1e9+7;
const ll N=1e5 + 10;
const ll M=2e5 + 10;
typedef pair<int,int> PII;
int n,m,k,cnt=1;
bool flag;

int main()
{
	cin>>n>>m>>k;
	for(int i=1;i<=k;i++)
	{
		stack<int>s;
		flag=0;
		cnt=1;
		for(int j=1;j<=n;j++)
		{
			int x;
			cin>>x;
			if(x==cnt)
			{
				cnt++;
				if(cnt==n)
				{
					flag=1;
				}
				while(cnt<n&&s.size()&&s.top()==cnt)
				{
					cnt++;
					s.pop();
					if(cnt==n)
					{
						flag=1;
					}
				}
			}
			else
			{
				if(s.size()<m)
				{
					s.push(x);
				}
				else
				{
					flag=0;
				}
			}
		}
        if(flag)puts("YES");
		else puts("NO");
	}
	return 0;
}

L2-034 口罩发放

#include <bits/stdc++.h>
using namespace std;
#define LL __int128
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
const ll lmax=2e18;
const ll mod =1e9+7;
const ll N=1e5 + 10;
const ll M=2e5 + 10;
typedef pair<int,int> PII;
unordered_map<string,int>mp,mp_v;
int d,p;

struct NODE
{
	string name,df,str_tim;
	int body;
	int tim;
	int no;
	bool operator<(const NODE&x)const
	{
		if(tim!=x.tim)
		return tim<x.tim;
		else return no<x.no;
	}
}nd;
vector<NODE>node;
vector<NODE>v;
int cal(string str)
{
	int h = stoi(str.substr(0,2));
	int m = stoi(str.substr(3,2));
	return h*60*60+m*60;
}
int main()
{
	cin>>d>>p;
	for(int i=1;i<=d;i++)
	{
		int t,s;
		cin>>t>>s;
		node.clear();
		for(int j=1;j<=t;j++)
		{
			string name,df,tim;
			int body;
			cin>>name>>df>>body>>tim;
			nd.name=name;
			nd.df = df;
			nd.body = body;
			nd.str_tim = tim;
			nd.tim = cal(tim);
			nd.no = j;
			bool flag=0;
			int tem = nd.df.size();
            for(int h=0;h<tem;h++)
            {
                if(!isdigit(nd.df[h]))flag = 1;//不是'0'~'9'
            }
            if(nd.df.size()!=18)
			{
				flag=1;
			}
            if(!flag)node.push_back(nd);
            if(!flag&&nd.body&&mp_v[nd.df]==0)
            {
            	mp_v[nd.df]=1;
				v.push_back({nd});
			}
		}
		sort(node.begin(),node.end());
		int tem = node.size();
		for(int j=0;j<tem;j++)
		{
            if(s==0)break;
			if(mp[node[j].df]==0||mp[node[j].df]<=i)
			{
                --s;
				cout<<node[j].name<<" "<<node[j].df<<endl;
				mp[node[j].df]=i+p+1;		
			}
		}

	}
	for(int i=0;i<v.size();i++)
	{
		cout<<v[i].name<<" "<<v[i].df<<endl;
	}
	return 0;
}

L2-035 完全二叉树的层序遍历

#include <bits/stdc++.h>
using namespace std;
#define LL __int128
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
const ll lmax=2e18;
const ll mod =1e9+7;
const ll N=1e5 + 10;
const ll M=2e5 + 10;
typedef pair<int,int> PII;
int n;
int a[33];
void dfs(int x)
{
	if(x>n)return;
	dfs(2*x);
	dfs(2*x+1);
	cin>>a[x];
}
int main()
{
	cin>>n;
	dfs(1);
	for(int i=1;i<=n;i++)
	{
		if(i!=1)cout<<" ";
		cout<<a[i];
	}
	return 0;
}

L2-036 网红点打卡攻略

#include <bits/stdc++.h>
using namespace std;
#define LL __int128
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
const ll lmax=2e18;
const ll mod =1e9+7;
const ll N=1e5 + 10;
const ll M=2e5 + 10;
typedef pair<int,int> PII;
int n,m,fa[N],head[N],cnt,ans=0x3f3f3f3f,mn=0x7f7f7f7f;
int edge[250][250],tot;
bool vis[250];
int f_ind(int x)
{
	return fa[x]=x==fa[x]?x:f_ind(fa[x]);
 } 
int main()
{
	cin>>n>>m;
	memset(edge,0x3f,sizeof edge);
	for(int i=1;i<=m;i++)
	{
		int x,y,w;
		cin>>x>>y>>w;
		edge[x][y]=w;
		edge[y][x]=w;
	}
	int k;
	cin>>k;
	for(int i=1;i<=k;i++)
	{
		int tem,las=0,sum=0;
		bool flag=0;
		cin>>tem;
		for(int j=0;j<=n;j++)fa[j]=j;
		memset(vis,0,sizeof vis);
		for(int j=1;j<=tem;j++)
		{
			int y;
			cin>>y;
			if(vis[y])flag=1;
			vis[y]=1;
			if(edge[las][y]==0x3f3f3f3f)
				flag=1;
			else
			{
				sum+=edge[las][y];
				fa[f_ind(las)] = f_ind(y);
			}
			las = y;
		}
		if(edge[las][0]==0x3f3f3f3f)
			flag=1;
		else
		{
			sum+=edge[las][0];
			fa[f_ind(las)] = f_ind(0);
		}
		
		int tmp = f_ind(0);
		for(int j=1;j<=n;j++)
		{
			if(f_ind(j)!=tmp)
			{
				flag=1;
				break;
			}
		}
		if(!flag)
		{
			tot++;
			if(mn>sum)
			{
				mn = sum;
				ans = i;
			}
		}
	}
	cout<<tot<<endl;
	cout<<ans<<" "<<mn;
	return 0;
}

L2-037 包装机

#include <bits/stdc++.h>
using namespace std;
#define LL __int128
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
const ll lmax=2e18;
const ll mod =1e9+7;
const ll N=110;
const ll M=2e5 + 10;
typedef pair<int,int> PII;
int n,m,s;
string str[N];
int cnt[N];//每个通道的指针
stack<char>kg;
int main()
{
	cin>>n>>m>>s;
	for(int i=1;i<=n;i++)cin>>str[i];
	int x;
	while(cin>>x)
	{
		if(x==-1)return 0;
		if(x!=0)
		{
			if(kg.size()==s&&cnt[x]<m)//框满了 
			{
				cout<<kg.top();
				kg.pop();
				kg.push(str[x][cnt[x]++]); 
			}
			else if(kg.size()==s&&cnt[x]==m)continue;//如果货架上没有 
			else//框没满 
			{
				if(cnt[x]==m)continue;//货架没有
				kg.push(str[x][cnt[x]++]);
			}
		}
		else
		{
			if(kg.size()==0)continue;
			else
			{
				cout<<kg.top();
				kg.pop();
			}
		}
		
	} 
	return 0;
}

L2-038 病毒溯源

#include <bits/stdc++.h>
using namespace std;
#define LL __int128
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
const ll lmax=2e18;
const ll mod =1e9+7;
const ll N=1e4 + 10;
const ll M=2e7 + 10;
typedef pair<int,int> PII;
int n,root,max_dis;
int head[N],cnt,in[N],dis[N];
bool vis[N];
string ans;
struct EDGE
{
	int x,y,neaxty;
}edge[M];
void addedge(int x,int y)
{
	edge[++cnt].x=x;
	edge[cnt].y=y;
	edge[cnt].neaxty=head[x];
	head[x]=cnt;
}
struct NODE
{
	int x,step;
	string str;
};
void bfs()
{
	queue<NODE>q;
	string str_tem;
	q.push({root,0,str_tem});
	while(q.size())
	{
		int x = q.front().x;
		int step = q.front().step;
		string str = q.front().str;
		q.pop();
		if(step>max_dis)
		{
			max_dis = step;
			ans = str;
		}
		else if(step == max_dis)
		{
			if(str<ans)ans = str;
		}
		for(int i=head[x];i;i=edge[i].neaxty)
		{
			int y = edge[i].y;
			q.push({y,step+1,str+" "+to_string(y)});
		}
	}
}
//之前用dj判断离源头最远的病毒的距离,然后用dfs搜索(层数确定了更方便返回?,不然不知道如何记录变异链),结果超时了
//但bfs维护一个vector或者string就很容易
int main()
{
	cin>>n;
	for(int i=0;i<n;i++)
	{
		int k;
		cin>>k;
		for(int j=1;j<=k;j++)
		{
			int y;
			cin>>y;
			addedge(i,y);
			in[y]++;
		}
	}
	for(int i=0;i<n;i++)
	{
		if(in[i]==0)
		{
			root=i;
			break;
		}
	}
	bfs();
    cout<<max_dis+1<<endl;
    ans = to_string(root) + ans;
    cout<<ans;
	return 0;
}

L2-039 清点代码库(map可以直接对不同的vector进行排序,且可以用vector作为键值)

#include <bits/stdc++.h>
using namespace std;
#define LL __int128
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
const ll lmax=2e18;
const ll mod =1e9+7;
const ll N=1e5 + 10;
const ll M=2e5 + 10;
typedef pair<int,int> PII;
#define v1w 1
#define v2w 0
map<vector<int>,int>mp;
int n,m,sum;
struct NODE
{
	int no,num;
	vector<int>v;
	bool operator<(const NODE&x)const
	{
		if(num!=x.num)
			return num>x.num;
		else return no<x.no;
	}
}node;
vector<NODE>v_ans;
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		vector<int>v_tem;
		for(int j=1;j<=m;j++)
		{
			int x;
			cin>>x;
			v_tem.push_back(x);
		}
		if(mp[v_tem]==0)
			sum++;
		mp[v_tem]++;
	}
	
	cout<<sum<<endl;
	int cnt=0;
	for(auto & t : mp)//这里的输出是按照键值来排序的,即vector已经排好序了(从小到大)
	{
		++cnt;
		v_ans.push_back({cnt,t.second,t.first}); 
	}
	sort(v_ans.begin(),v_ans.end());
	for(int i=0;i<v_ans.size();i++)
	{
		if(i!=0)cout<<endl;
		cout<<v_ans[i].num;
		for(int j=0;j<v_ans[i].v.size();j++)
		{
			cout<<" "<<v_ans[i].v[j];
		}
	}
	return 0;
}

L2-040 哲哲打游戏

#include <bits/stdc++.h>
using namespace std;
#define LL __int128
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
const ll lmax=2e18;
const ll mod =1e9+7;
const ll N=1e5 + 10;
const ll M=1e6 + 10;
typedef pair<int,int> PII;
int n,m;
int cd[110];
int ev[N];
PII opr[N];
int tot;
vector<int>v[N];
bool flag=0;

void dfs(int x)
{
	while(++tot<=m)
	{
		if(opr[tot].first == 0)
		{
			int y = opr[tot].second-1;
			dfs(v[x][y]);
		}
		else if(opr[tot].first == 1)
		{
			cd[opr[tot].second]=x;
			printf("%d\n",x);
		}
		else
		{
			int j = opr[tot].second;
			if(cd[j])
				dfs(cd[j]);
			else 
				dfs(1); 
		}
	}
	if(tot>m&&!flag)
	{
		printf("%d",x);
		flag=1;
	}
}
int main()
{
	scanf("%d %d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		int k;
		scanf("%d",&k);
		ev[i]=k;
		for(int j=1;j<=k;j++)
		{
			int y;
			scanf("%d",&y);
			v[i].push_back(y);
		}
	}
	for(int i=1;i<=m;i++)
	{
		scanf("%d %d",&opr[i].first,&opr[i].second);
	}
	dfs(1);
	return 0;
}

L3-001 凑零钱

#include <bits/stdc++.h>
using namespace std;
#define LL __int128
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
const ll lmax=2e18;
const ll mod =1e9+7;
const ll N=1e4 + 10;
const ll M=2e5 + 10;
typedef pair<int,int> PII;
int dp[110],n,m;
int a[N];
bool vis[N];
vector<int>ans;
bool dfs(int sum)
{
	if(sum==0)
	{
		for(int i=1;i<=n;i++)
		{
			if(vis[i])ans.push_back(a[i]);
		}
		return 1;
	}
	for(int i=1;i<=n;i++)
	{
		if(vis[i])continue;
		if(sum-a[i]>=0&&dp[sum-a[i]])
		{
			vis[i]=1;
			if(dfs(sum-a[i]))return 1;
			vis[i]=0;
		}
	}
	return 0;
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)cin>>a[i];
	dp[0]=1;
	for(int i=1;i<=n;i++)
	{
		for(int j=m;j>=a[i];j--)
		{
			if(dp[j-a[i]])
			{
				dp[j]=1;
			}
		}
	}
	if(!dp[m])cout<<"No Solution"<<endl;
	else
	{
		sort(a+1,a+1+n);
		memset(vis,0,sizeof vis);
		dfs(m);
		for(int i=0;i<ans.size();i++)
		{
			if(i!=0)cout<<" ";
			cout<<ans[i];
		}
	}
	
	return 0;
}

L3-003 社交集群

#include <bits/stdc++.h>
using namespace std;
#define LL __int128
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
const ll lmax=2e18;
const ll mod =1e9+7;
const ll N=1e4 + 10;
const ll M=2e5 + 10;
typedef pair<int,int> PII;
int n;
int fa[N],sum;
vector<int>ans;
unordered_map<int,int>mp,vis;
int f_ind(int x)
{
	return fa[x]=fa[x]==x?x:f_ind(fa[x]);
}
int main()
{
	cin>>n;
	for(int i=1;i<N;i++)fa[i]=i;
	for(int i=1;i<=n;i++)
	{
		int k;
		char ch;
		cin>>k>>ch;
		for(int j=1;j<=k;j++)
		{
			int x;
			cin>>x;
			x+=n;
			fa[f_ind(i)] = f_ind(x);
		}
	}
	for(int i=1;i<=n;i++)
	{
		int x = f_ind(i);
		mp[x]++;
	}
	cout<<mp.size()<<endl;
	for(auto &t : mp)
	{
		ans.push_back(t.second);
	}
	sort(ans.begin(),ans.end(),greater<int>());
	for(int i=0;i<ans.size();i++)
	{
		if(i!=0)cout<<" ";
		cout<<ans[i];
	}
	return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值