101512024-03-28 17:44:52111Turista járatokcpp17Accepted 100/100212ms91988 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N,M,K;
	cin>>N>>M>>K;
	vector<vector<int>>g(N+1),t(N+1);
	vector<pair<int,int>>e;
	for(int i=0;i<M;i++){
		int a,b;
		cin>>a>>b;
		if(i<K){
			t[a].push_back(b);
			t[b].push_back(a);
			e.emplace_back(a,b);
			e.emplace_back(b,a);
		}
		else{
			g[a].push_back(b);
			g[b].push_back(a);
		}
	}
	if(0){
start:
		int TREE=0;
		N=2+rand()%20,M=TREE?N-1:rand()%min(N*(N-1)/2,N*2),K=M==0?0:rand()%M;
		struct DSU {
			vector<int> v;

			DSU(int n) : v(n + 1, -1) {
			}

			int find(int a) {
				return v[a] < 0 ? a : v[a] = find(v[a]);
			}

			void unite(int a, int b) {
				a = find(a);
				b = find(b);
				if (a == b) {
					return;
				}
				if (v[a] > v[b]) {
					swap(a, b);
				}
				v[a] += v[b];
				v[b] = a;
			}
		};
		DSU dsu(N+1);
		g.clear();
		g.resize(N+1);
		t.clear();
		t.resize(N+1);
		e.clear();
		set<pair<int,int>>s;
		for(int i=0;i<M;i++){
			int a,b;
			do{
				a=1+rand()%N,b=1+rand()%N;
			}
			while(a==b||s.count({a,b})||(TREE&&dsu.find(a)==dsu.find(b)));
			s.insert({a,b});
			s.insert({b,a});
			dsu.unite(a,b);
			if(i<K){
				t[a].push_back(b);
				t[b].push_back(a);
				e.emplace_back(a,b);
				e.emplace_back(b,a);
			}
			else{
				g[a].push_back(b);
				g[b].push_back(a);
			}
		}
	}
	vector<vector<pair<int,int>>>gg(N+1);
	for(int i=1;i<=N;i++){
		for(int j:g[i]){
			gg[i].emplace_back(j,0);
		}
		for(int j:t[i]){
			gg[i].emplace_back(j,1);
		}
	}
#define g gg
	vector<int>v(N+1),w(N+1),u(N+1),z(N+1),p(N+1);
	auto dfs=[&](auto self,int i)->set<int>{
		set<int>ss;
		w[i]=i;
		for(auto[j,b]:g[i]){
			if(j==p[i]){
				continue;
			}
			if(v[j]){
				if(v[j]<v[w[i]]){
					w[i]=j;
				}
				continue;
			}
			v[j]=v[i]+1;
			p[j]=i;
			auto zz=self(self,j);
			if(w[j]==j||w[j]==i){
				int sb=0;
				for(int k:zz)for(auto[jj,b]:g[k])sb|=b&&(zz.count(jj)||jj==w[j]||jj==j);
				if(sb){
					for(int k:zz)z[k]|=1;
					z[j]|=w[j]==i;
				}
				sb=0;
			}
			else{
				zz.insert(j);
				if(zz.size()>ss.size()){
					swap(ss,zz);
				}
				ss.insert(zz.begin(),zz.end());
			}
			if(v[w[j]]<v[w[i]]){
				w[i]=w[j];
			}
			if(b){
				u[j]=1;
			}
		}
		return ss;
	};
	v[1]=1;
	dfs(dfs,1);
	auto dfs2=[&](auto self,int i,int b)->void{
		z[i]|=b;
		for(auto[j,x]:g[i]){
			if(j==p[i]){
				continue;
			}
			if(v[j]&&p[j]!=i){
				continue;
			}
			v[j]=v[i]+1;
			p[j]=i;
			self(self,j,b|u[j]|z[j]|(z[i]&&w[j]==j));
		}
	};
	dfs2(dfs2,1,0);
#undef g
	if(0){
		int st=clock();
		vector<int>ans;
		for(int i=1;i<=N;i++){
			if(z[i])ans.push_back(i);
		}
		vector<int>ans1;
		{
			vector<int>v(N+1),u(N+1);
			auto bt=[&](auto self,int i,int b)->void{
				if(clock()>st+CLOCKS_PER_SEC/10){
					return;
				}
				if(v[i]){
					return;
				}
				if(b){
					u[i]=1;
				}
				v[i]=1;
				for(int j:g[i]){
					self(self,j,b);
				}
				for(int j:t[i]){
					self(self,j,1);
				}
				v[i]=0;
			};
			bt(bt,1,0);
			if(clock()>st+CLOCKS_PER_SEC/10){
				cout<<"TIMEOUT"<<endl;
				goto start;
			}
			for(int i=1;i<=N;i++){
				if(u[i]){
					ans1.push_back(i);
				}
			}
		}
		if(ans1.size()!=ans.size()){
			cout<<"============= INPUT ============="<<endl;
			cout<<N<<" "<<M<<" "<<K<<endl;
			for(int i=1;i<=N;i++){
				for(int j:t[i]){
					if(j<i)continue;
					cout<<i<<' '<<j<<' '<<endl;
				}
			}
			for(int i=1;i<=N;i++){
				for(int j:g[i]){
					if(j<i)continue;
					cout<<i<<' '<<j<<endl;
				}
			}
			cout<<"============= NODES ============="<<endl;
			for(int i=1;i<=N;i++){
				cout<<setw(3)<<i<<" L "<<v[i]<<" D "<<w[i]<<" P "<<p[i]<<endl;
			}
			cout<<"============= EDGES ============="<<endl;
			for(int i=1;i<=N;i++){
				for(int j:g[i]){
					if(j<i)continue;
					int xx=(p[j]==i||p[i]==j?min(v[i],v[j]):0);
					cout<<i<<' '<<j<<' '<<0<<endl;
				}
				for(int j:t[i]){
					if(j<i)continue;
					int xx=(p[j]==i||p[i]==j?min(v[i],v[j]):0);
					cout<<i<<' '<<j<<' '<<1<<endl;
				}
			}
			cout<<"============= GOT ============="<<endl;
			cout<<ans.size()<<'\n';
			for(int i:ans){
				cout<<i<<' ';
			}
			cout<<'\n';
			cout<<"============= EXP ============="<<endl;
			cout<<ans1.size()<<'\n';
			for(int i:ans1){
				cout<<i<<' ';
			}
			cout<<'\n';
			exit(1);
		}
		static int cc=0;
		cout<<"OK "<<setw(9)<<cc++<<" "<<ans.size()<<endl;
		goto start;
	}
	cout<<count(z.begin()+1,z.end(),1)<<'\n';
	for(int i=1;i<=N;i++){
		if(z[i]){
			cout<<i<<' ';
		}
	}
	cout<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1892 KiB
2Accepted14ms7044 KiB
subtask210/10
3Accepted3ms2300 KiB
4Accepted3ms2524 KiB
5Accepted3ms2612 KiB
6Accepted3ms2768 KiB
7Accepted3ms3048 KiB
subtask310/10
8Accepted3ms3228 KiB
9Accepted3ms3124 KiB
10Accepted3ms3272 KiB
11Accepted3ms3920 KiB
12Accepted6ms5400 KiB
subtask410/10
13Accepted9ms7680 KiB
14Accepted3ms3708 KiB
15Accepted4ms4316 KiB
16Accepted4ms4884 KiB
17Accepted212ms90888 KiB
18Accepted107ms30700 KiB
19Accepted134ms35556 KiB
20Accepted100ms30308 KiB
subtask510/10
21Accepted13ms8768 KiB
22Accepted3ms4488 KiB
23Accepted3ms4648 KiB
24Accepted3ms4788 KiB
25Accepted105ms48456 KiB
26Accepted14ms8628 KiB
27Accepted14ms8572 KiB
subtask660/60
28Accepted4ms5076 KiB
29Accepted4ms5236 KiB
30Accepted4ms5540 KiB
31Accepted6ms6300 KiB
32Accepted7ms6788 KiB
33Accepted8ms7264 KiB
34Accepted14ms9456 KiB
35Accepted14ms9792 KiB
36Accepted13ms9664 KiB
37Accepted192ms91988 KiB
38Accepted98ms30260 KiB
39Accepted107ms31224 KiB
40Accepted105ms31508 KiB
41Accepted98ms30380 KiB
42Accepted101ms31496 KiB
43Accepted104ms30764 KiB
44Accepted104ms30660 KiB
45Accepted135ms36240 KiB
46Accepted104ms31356 KiB
47Accepted97ms29868 KiB
48Accepted100ms30976 KiB
49Accepted104ms31076 KiB
50Accepted105ms31400 KiB