第二十四个点WA掉了
查看原帖
第二十四个点WA掉了
476487
tangml楼主2024/7/22 09:22
#include<bits/stdc++.h>
#define int long long 
#define lowbit(x) x&-x
#define pb push_back

using namespace std;

const int N=1e6+100,M=5e6+10;

int n,m,k;
int depth[N],fa[N];
int e[M*2],h[N],ne[M*2],idx;
bool vis[N];
bool leaf[N];
vector<int> E[N];

void add(int a,int b)
{
	e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}

void dfs(int p,int father)
{
	depth[p]=depth[father]+1;
	fa[p]=father;
	vis[p]=true;
	bool flag=false;
	for(int i=h[p];~i;i=ne[i])
	{
		int j=e[i];
		if(j==father)	continue;
		if(!vis[j])
			flag=true,dfs(j,p);
		else if(depth[p]>depth[j])
			E[p].pb(j);
	}
	if(depth[p]>=ceil(n/k))
	{
		cout<<"PATH"<<endl<<depth[p]<<endl;
		while(p)	cout<<p<<" ",p=fa[p];
		exit(0);
	}
	if(!flag)	leaf[p]=1;
}

void print(int p)
{
	int f=E[p][0];
	int gf=E[p][1];
	
	if(depth[f]<depth[gf])	swap(f,gf);
	
	if((depth[p]-depth[f]+1)%3) 
	{
		cout<<depth[p]-depth[f]+1<<endl;
		while(p!=fa[f])
			cout<<p<<" ",p=fa[p];
		cout<<endl; 
	}
	
	else if((depth[p]-depth[gf]+1)%3)
	{
		cout<<depth[p]-depth[gf]+1<<endl;
		while(p!=fa[gf])
			cout<<p<<" ",p=fa[p];
		cout<<endl; 
	}
	else
	{
		cout<<depth[p]-depth[gf]+2<<endl<<p<<" ";
		p=f;
		while(p!=fa[gf])
			cout<<p<<" ",p=fa[p];
		cout<<endl; 
	}
}

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	
	memset(h,-1,sizeof h);
	
	cin>>n>>m>>k;
	
	for(int i=1;i<=m;i++)
	{
		int x,y;
		cin>>x>>y;
		add(x,y),add(y,x);
	}
	
	dfs(1,0);
	
	cout<<"CYCLES"<<endl;
	
	int cnt=0;
	
	for(int i=1;i<=n;i++)
	{
		if(leaf[i])	print(i),cnt++;
		if(cnt==k)	break;
	}
	
	return 0;
}
2024/7/22 09:22
加载中...