Submission #1058379

# Submission time Handle Problem Language Result Execution time Memory
1058379 2024-08-14T09:41:33 Z shenfe1 Simurgh (IOI17_simurgh) C++17
70 / 100
256 ms 15564 KB
#include <bits/stdc++.h>
#include "simurgh.h"

#pragma GCC optimize("Ofast")

using namespace std;

#define pb push_back
#define pii pair<int,int>
#define sz(s) (int)s.size()
#define F first
#define S second
#define all(v) v.begin(),v.end()

const int MAX=2e5+10;

struct dsu{
	int f[MAX];

	void init(int n){
		for(int i=0;i<n;i++)f[i]=i;
	}

	int get(int v){
		return (f[v]==v?v:f[v]=get(f[v]));
	}

	void unite(int a,int b){
		a=get(a),b=get(b);
		f[a]=b;
	}
}d;

int n;
vector<pii> g[MAX];
vector<int> u,v;
int is1[MAX],is0[MAX],dn[MAX];
int pr[MAX],dep[MAX];
set<int> st;

void dfs(int st){
	queue<int> q;
	q.push(st);
	while(!q.empty()){
		int v=q.front();
		q.pop();
		for(auto to:g[v]){
			if(pr[v]==to.S)continue;
			dep[to.F]=dep[v]+1;
			pr[to.F]=to.S;
			q.push(to.F);
		}
	}
}

vector<int> vc(set<int> &st){
	vector<int> res;
	for(int i:st)res.pb(i);
	return res;
}

int add(vector<int> &vec){
	int CNT=0;
	d.init(n);
	vector<int> x;
	for(int i:vec){
		x.pb(i);
		d.unite(u[i],v[i]);
	}
	for(int i:st){
		if(d.get(u[i])!=d.get(v[i])){
			CNT+=is1[i];
			d.unite(u[i],v[i]);
			x.pb(i);
		}
	}
	return count_common_roads(x)-CNT;
}

vector<int> find_roads(int N,vector<int> U,vector<int> V){
	n=N;
	u=U;
	v=V;
	vector<int> e;
	d.init(n);
	for(int i=0;i<sz(U);i++){
		if(d.get(U[i])!=d.get(V[i])){
			d.unite(U[i],V[i]);
			g[U[i]].pb({V[i],i});
			g[V[i]].pb({U[i],i});
			st.insert(i);
		}
		else e.pb(i);
	}
	for(int i=0;i<sz(U);i++)dn[i]=1;
	for(int i=0;i<n;i++)pr[i]=-1;
	dfs(0);
	int cnt=count_common_roads(vc(st));
	for(int num:e){
		int a=U[num],b=V[num];
		vector<int> canzam;
		int allv=0;
		int CNT=0;
		while(a!=b){
			CNT++;
			if(CNT>N){
				return vector<int>(n,-1);
			}
			if(dep[a]>dep[b]){
				canzam.pb(pr[a]);
				if(U[pr[a]]==a)a=V[pr[a]];
				else a=U[pr[a]];
			}
			else{
				canzam.pb(pr[b]);
				if(U[pr[b]]==b)b=V[pr[b]];
				else b=U[pr[b]];
			}
		}
		int ok=0;
		int canEdge=-1;
		for(int edge:canzam){
			if(dn[edge])allv=1;
			if(is1[edge]){
				ok=1;
				canEdge=edge;
			}
			else if(is0[edge]){
				ok=2;
				canEdge=edge;
			}
		}
		if(!allv)continue;
		if(ok==0){
			for(int edge:canzam){
				assert(dn[edge]);
				st.erase(edge);
				st.insert(num);
				int ncnt=count_common_roads(vc(st));
				if(ncnt<cnt){
					dn[edge]=0;
					is1[edge]=1;
					dn[num]=0;
					is0[num]=1;
				}
				else if(ncnt>cnt){
					dn[edge]=0;
					is0[edge]=1;
					dn[num]=0;
					is1[num]=1;
				}
				st.insert(edge);
				st.erase(num);
			}
			if(dn[num]){
				dn[num]=0;
				is0[num]=1;
				for(int edge:canzam){
					dn[edge]=0;
					is0[edge]=1;
				}
			}
			else{
				for(int edge:canzam){
					if(dn[edge]){
						dn[edge]=0;
						is0[edge]=is0[num];
						is1[edge]=is1[num];
					}
				}
			}
		}
		else if(ok==1){
			st.erase(canEdge);
			st.insert(num);
			int ncnt=count_common_roads(vc(st));
			st.insert(canEdge);
			st.erase(num);
			dn[num]=0;
			if(ncnt==cnt){
				is1[num]=1;
			}
			else{
				is0[num]=1;
			}
		}
		else{
			st.erase(canEdge);
			st.insert(num);	
			int ncnt=count_common_roads(vc(st));
			st.insert(canEdge);
			st.erase(num);
			dn[num]=0;
			if(ncnt==cnt){
				is0[num]=1;
			}
			else{
				is1[num]=1;
			}
		}
		assert(is0[num]||is1[num]);
		for(int edge:canzam){
			if(dn[edge]){
				dn[edge]=0;
				st.insert(num);
				st.erase(edge);
				int ncnt=count_common_roads(vc(st));
                st.erase(num);
				st.insert(edge);
				if(ncnt==cnt){
					is0[edge]=is0[num];
					is1[edge]=is1[num];
				}
				else{
					is0[edge]=1-is0[num];
					is1[edge]=1-is1[num];
				}
			}
		}
		if(is1[num]){
			cnt++;
			for(int edge:canzam){
				if(is0[edge]){
					st.erase(edge);
					st.insert(num);
					break;
				}
			}
		}
		for(int i=0;i<n;i++){
			g[i].clear();
			pr[i]=-1;
		}
		for(int i:st){
			g[U[i]].pb({V[i],i});
			g[V[i]].pb({U[i],i});
		}
		dfs(0);
	}
	for(int edge:st){
		if(dn[edge]){
			dn[edge]=0;
			is1[edge]=1;
		}
	}
	for(int i=0;i<n;i++){
		g[i].clear();
	}
	for(int i=0;i<sz(u);i++){
		g[u[i]].pb({v[i],i});
		g[v[i]].pb({u[i],i});
	}
	for(int i=0;i<n;i++){
		vector<int> vec;
		for(auto to:g[i]){
			if(!st.count(to.S))vec.pb(to.S);
		}
		int cnt=add(vec);
		// cout<<i<<" "<<cnt<<" "<<vec[0]<<"\n";
		for(int j=1;j<=cnt;j++){
			int l=0,r=sz(g[i])-1,res=sz(g[i])-1;
			while(l<=r){
				int mid=(l+r)/2;
				vector<int> vec;
				for(int j=0;j<=mid;j++){
					if(!st.count(g[i][j].S))vec.pb(g[i][j].S);
				}
				if(add(vec)>=j){
					r=mid-1;
					res=mid;
				}
				else l=mid+1;
			}
			is1[g[i][res].S]=1;
			// cout<<g[i][res].S<<"\n";
		}
	}
	vector<int> ans;
	for(int i=0;i<sz(u);i++)if(is1[i])ans.pb(i);
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8792 KB correct
2 Correct 1 ms 8796 KB correct
3 Correct 1 ms 8792 KB correct
4 Correct 1 ms 8796 KB correct
5 Correct 2 ms 8796 KB correct
6 Correct 2 ms 8796 KB correct
7 Correct 1 ms 8796 KB correct
8 Correct 1 ms 8796 KB correct
9 Correct 1 ms 8796 KB correct
10 Correct 1 ms 8796 KB correct
11 Correct 1 ms 8796 KB correct
12 Correct 2 ms 8796 KB correct
13 Correct 1 ms 8796 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8792 KB correct
2 Correct 1 ms 8796 KB correct
3 Correct 1 ms 8792 KB correct
4 Correct 1 ms 8796 KB correct
5 Correct 2 ms 8796 KB correct
6 Correct 2 ms 8796 KB correct
7 Correct 1 ms 8796 KB correct
8 Correct 1 ms 8796 KB correct
9 Correct 1 ms 8796 KB correct
10 Correct 1 ms 8796 KB correct
11 Correct 1 ms 8796 KB correct
12 Correct 2 ms 8796 KB correct
13 Correct 1 ms 8796 KB correct
14 Correct 3 ms 8796 KB correct
15 Correct 3 ms 8992 KB correct
16 Correct 2 ms 8796 KB correct
17 Correct 3 ms 8796 KB correct
18 Correct 2 ms 8796 KB correct
19 Correct 4 ms 9048 KB correct
20 Correct 3 ms 8796 KB correct
21 Correct 3 ms 8796 KB correct
22 Correct 1 ms 8796 KB correct
23 Correct 2 ms 8792 KB correct
24 Correct 2 ms 8796 KB correct
25 Correct 1 ms 8908 KB correct
26 Correct 2 ms 9052 KB correct
27 Correct 2 ms 8792 KB correct
28 Correct 1 ms 8888 KB correct
29 Correct 2 ms 8896 KB correct
30 Correct 2 ms 8796 KB correct
31 Correct 2 ms 8928 KB correct
32 Correct 2 ms 8796 KB correct
33 Correct 1 ms 8940 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8792 KB correct
2 Correct 1 ms 8796 KB correct
3 Correct 1 ms 8792 KB correct
4 Correct 1 ms 8796 KB correct
5 Correct 2 ms 8796 KB correct
6 Correct 2 ms 8796 KB correct
7 Correct 1 ms 8796 KB correct
8 Correct 1 ms 8796 KB correct
9 Correct 1 ms 8796 KB correct
10 Correct 1 ms 8796 KB correct
11 Correct 1 ms 8796 KB correct
12 Correct 2 ms 8796 KB correct
13 Correct 1 ms 8796 KB correct
14 Correct 3 ms 8796 KB correct
15 Correct 3 ms 8992 KB correct
16 Correct 2 ms 8796 KB correct
17 Correct 3 ms 8796 KB correct
18 Correct 2 ms 8796 KB correct
19 Correct 4 ms 9048 KB correct
20 Correct 3 ms 8796 KB correct
21 Correct 3 ms 8796 KB correct
22 Correct 1 ms 8796 KB correct
23 Correct 2 ms 8792 KB correct
24 Correct 2 ms 8796 KB correct
25 Correct 1 ms 8908 KB correct
26 Correct 2 ms 9052 KB correct
27 Correct 2 ms 8792 KB correct
28 Correct 1 ms 8888 KB correct
29 Correct 2 ms 8896 KB correct
30 Correct 2 ms 8796 KB correct
31 Correct 2 ms 8928 KB correct
32 Correct 2 ms 8796 KB correct
33 Correct 1 ms 8940 KB correct
34 Correct 59 ms 10308 KB correct
35 Correct 64 ms 10332 KB correct
36 Correct 44 ms 10072 KB correct
37 Correct 13 ms 8992 KB correct
38 Correct 50 ms 10332 KB correct
39 Correct 54 ms 10328 KB correct
40 Correct 41 ms 10076 KB correct
41 Correct 68 ms 10332 KB correct
42 Correct 51 ms 10328 KB correct
43 Correct 10 ms 9820 KB correct
44 Correct 8 ms 9564 KB correct
45 Correct 10 ms 9476 KB correct
46 Correct 9 ms 9308 KB correct
47 Correct 5 ms 9052 KB correct
48 Correct 3 ms 8796 KB correct
49 Correct 5 ms 9020 KB correct
50 Correct 5 ms 9232 KB correct
51 Correct 9 ms 9564 KB correct
52 Correct 8 ms 9412 KB correct
53 Correct 8 ms 9400 KB correct
54 Correct 12 ms 9820 KB correct
55 Correct 10 ms 9564 KB correct
56 Correct 10 ms 9712 KB correct
57 Correct 10 ms 9564 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8796 KB correct
2 Correct 1 ms 8796 KB correct
3 Correct 156 ms 13388 KB correct
4 Correct 256 ms 15376 KB correct
5 Correct 252 ms 15308 KB correct
6 Correct 231 ms 15304 KB correct
7 Correct 233 ms 15480 KB correct
8 Correct 244 ms 15460 KB correct
9 Correct 244 ms 15468 KB correct
10 Correct 253 ms 15564 KB correct
11 Correct 243 ms 15468 KB correct
12 Correct 255 ms 15564 KB correct
13 Correct 2 ms 8796 KB correct
14 Correct 229 ms 15484 KB correct
15 Correct 254 ms 15304 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8792 KB correct
2 Correct 1 ms 8796 KB correct
3 Correct 1 ms 8792 KB correct
4 Correct 1 ms 8796 KB correct
5 Correct 2 ms 8796 KB correct
6 Correct 2 ms 8796 KB correct
7 Correct 1 ms 8796 KB correct
8 Correct 1 ms 8796 KB correct
9 Correct 1 ms 8796 KB correct
10 Correct 1 ms 8796 KB correct
11 Correct 1 ms 8796 KB correct
12 Correct 2 ms 8796 KB correct
13 Correct 1 ms 8796 KB correct
14 Correct 3 ms 8796 KB correct
15 Correct 3 ms 8992 KB correct
16 Correct 2 ms 8796 KB correct
17 Correct 3 ms 8796 KB correct
18 Correct 2 ms 8796 KB correct
19 Correct 4 ms 9048 KB correct
20 Correct 3 ms 8796 KB correct
21 Correct 3 ms 8796 KB correct
22 Correct 1 ms 8796 KB correct
23 Correct 2 ms 8792 KB correct
24 Correct 2 ms 8796 KB correct
25 Correct 1 ms 8908 KB correct
26 Correct 2 ms 9052 KB correct
27 Correct 2 ms 8792 KB correct
28 Correct 1 ms 8888 KB correct
29 Correct 2 ms 8896 KB correct
30 Correct 2 ms 8796 KB correct
31 Correct 2 ms 8928 KB correct
32 Correct 2 ms 8796 KB correct
33 Correct 1 ms 8940 KB correct
34 Correct 59 ms 10308 KB correct
35 Correct 64 ms 10332 KB correct
36 Correct 44 ms 10072 KB correct
37 Correct 13 ms 8992 KB correct
38 Correct 50 ms 10332 KB correct
39 Correct 54 ms 10328 KB correct
40 Correct 41 ms 10076 KB correct
41 Correct 68 ms 10332 KB correct
42 Correct 51 ms 10328 KB correct
43 Correct 10 ms 9820 KB correct
44 Correct 8 ms 9564 KB correct
45 Correct 10 ms 9476 KB correct
46 Correct 9 ms 9308 KB correct
47 Correct 5 ms 9052 KB correct
48 Correct 3 ms 8796 KB correct
49 Correct 5 ms 9020 KB correct
50 Correct 5 ms 9232 KB correct
51 Correct 9 ms 9564 KB correct
52 Correct 8 ms 9412 KB correct
53 Correct 8 ms 9400 KB correct
54 Correct 12 ms 9820 KB correct
55 Correct 10 ms 9564 KB correct
56 Correct 10 ms 9712 KB correct
57 Correct 10 ms 9564 KB correct
58 Correct 1 ms 8796 KB correct
59 Correct 1 ms 8796 KB correct
60 Correct 156 ms 13388 KB correct
61 Correct 256 ms 15376 KB correct
62 Correct 252 ms 15308 KB correct
63 Correct 231 ms 15304 KB correct
64 Correct 233 ms 15480 KB correct
65 Correct 244 ms 15460 KB correct
66 Correct 244 ms 15468 KB correct
67 Correct 253 ms 15564 KB correct
68 Correct 243 ms 15468 KB correct
69 Correct 255 ms 15564 KB correct
70 Correct 2 ms 8796 KB correct
71 Correct 229 ms 15484 KB correct
72 Correct 254 ms 15304 KB correct
73 Correct 1 ms 8796 KB correct
74 Incorrect 208 ms 15308 KB WA in grader: NO
75 Halted 0 ms 0 KB -