101452024-03-28 16:14:40111Turista járatokcpp17Wrong answer 0/100173ms91348 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()%10,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),p(N+1),ti(N+1),to(N+1);
	vector<array<int,16>>up(N+1);
	auto des=[&](int a,int b)->bool{
		return ti[a]<=ti[b]&&to[a]>=to[b];
	};
	auto lca=[&](int a,int b)->int{
		while(!des(a,b)){
			for(int i=1;;i++){
				if(des(up[a][i],b)){
					a=up[a][i-1];
					break;
				}
			}
		}
		return a;
	};
	auto lcb=[&](int a,int b)->int{
		if(des(a,b)){
			return -1;
		}
		while(!des(p[a],b)){
			for(int i=1;;i++){
				if(des(p[up[a][i]],b)){
					a=up[a][i-1];
					break;
				}
			}
		}
		return a;
	};
	int ta=1;
	auto dfs=[&](auto self,int i)->void{
		w[i]=i;
		up[i][0]=p[i];
		for(int j=1;j<16;j++){
			up[i][j]=up[up[i][j-1]][j-1];
		}
		ti[i]=ta++;
		for(auto[j,b]:g[i]){
			if(v[j]){
				if(v[j]<v[w[i]]){
					w[i]=j;
				}
				continue;
			}
			v[j]=v[i]+1;
			p[j]=i;
			self(self,j);
			if(v[w[j]]<v[w[i]]){
				w[i]=w[j];
			}
		}
		to[i]=ta++;
	};
	v[1]=1;
	dfs(dfs,1);
	ti[0]=0;
	to[0]=ta;
	auto dfs2=[&](auto self,int i)->void{
		for(auto[j,b]:g[i]){
			if(j==p[i]){
				continue;
			}
			if(v[j]>v[i]+1){
				continue;
			}
			if(v[j]<v[i]){
				continue;
			}
			if(b){
				u[j]=1;
			}
			self(self,j);
		}
	};
	dfs2(dfs2,1);
	auto dfs3=[&](auto self,int i,int b)->void{
		u[i]|=b;
		b|=u[i];
		for(auto[j,x]:g[i]){
			if(v[j]&&p[j]!=i){
				continue;
			}
			v[j]=v[i]+1;
			p[j]=i;
			self(self,j,b|x);
		}
	};
	dfs3(dfs3,1,0);
#undef g
	if(0){
		int st=clock();
		vector<int>ans;
		for(int i=1;i<=N;i++){
			if(u[i])ans.push_back(i);
		}
		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;
		}
		vector<int>ans1;
		for(int i=1;i<=N;i++){
			if(u[i]){
				ans1.push_back(i);
			}
		}
		if(ans1.size()!=ans.size()){
			for(int i=1;i<=N;i++){
				for(int j:g[i]){
					if(j<i)continue;
					cout<<i<<' '<<j<<' '<<0<<endl;
				}
				for(int j:t[i]){
					if(j<i)continue;
					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(u.begin(),u.end(),1)<<'\n';
	for(int i=1;i<=N;i++){
		if(u[i]){
			cout<<i<<' ';
		}
	}
	cout<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Wrong answer3ms1832 KiB
2Accepted14ms9592 KiB
subtask20/10
3Wrong answer3ms2376 KiB
4Wrong answer3ms2480 KiB
5Wrong answer3ms2616 KiB
6Wrong answer2ms2700 KiB
7Wrong answer3ms2724 KiB
subtask30/10
8Wrong answer3ms2712 KiB
9Accepted2ms2752 KiB
10Accepted3ms2772 KiB
11Accepted3ms3568 KiB
12Accepted7ms6172 KiB
subtask40/10
13Accepted10ms9520 KiB
14Accepted3ms3100 KiB
15Accepted4ms3748 KiB
16Accepted4ms4272 KiB
17Wrong answer173ms90308 KiB
18Wrong answer68ms34764 KiB
19Wrong answer92ms40488 KiB
20Wrong answer68ms34916 KiB
subtask50/10
21Accepted14ms11000 KiB
22Wrong answer3ms3736 KiB
23Wrong answer3ms4064 KiB
24Wrong answer3ms4172 KiB
25Wrong answer101ms47976 KiB
26Wrong answer12ms8396 KiB
27Wrong answer12ms8292 KiB
subtask60/60
28Wrong answer4ms4404 KiB
29Accepted4ms4524 KiB
30Accepted4ms5176 KiB
31Wrong answer6ms6304 KiB
32Wrong answer7ms6952 KiB
33Accepted8ms7988 KiB
34Accepted14ms11764 KiB
35Accepted14ms11844 KiB
36Accepted14ms11816 KiB
37Wrong answer173ms91348 KiB
38Wrong answer75ms35816 KiB
39Wrong answer76ms36644 KiB
40Wrong answer75ms36332 KiB
41Wrong answer71ms36372 KiB
42Wrong answer68ms36608 KiB
43Wrong answer76ms36600 KiB
44Wrong answer75ms36460 KiB
45Wrong answer101ms42364 KiB
46Wrong answer71ms36668 KiB
47Wrong answer68ms36668 KiB
48Wrong answer75ms36668 KiB
49Wrong answer75ms36520 KiB
50Wrong answer75ms36544 KiB