답안 #1082837

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1082837 2024-09-01T17:00:23 Z amirhoseinfar1385 Simurgh (IOI17_simurgh) C++17
100 / 100
806 ms 21072 KB
#include "simurgh.h"
#include<bits/stdc++.h>
using namespace std;
const int maxn=500+10,maxm=maxn*maxn;
vector<int>adj[maxn];
int n,m,vis[maxn],high[maxn],p[maxn];
pair<int,int>alle[maxm];
vector<int>dfsor;
map<pair<int,int>,int>lnk;
map<int,int>mp;
vector<int>res;
vector<int>adjt,updy;
struct dsu{
	int par[maxn];
	dsu(){
		for(int i=0;i<maxn;i++){
			par[i]=i;
		}
	}
	int root(int u){
		if(u==par[u]){
			return u;
		}
		return par[u]=root(par[u]);
	}
	int con(int u,int v){
		u=root(u);v=root(v);
		if(u==v){
			return 0;
		}
		par[u]=v;
		return 1;
	}
	void clear(){
		for(int i=0;i<maxn;i++){
			par[i]=i;
		}
	}
}ds;
int ted=0;

int pors(set<int>st){
	ted++;
	if(ted>=30000){
		exit(23);
	}
	vector<int>ers;
	for(auto x:st){
		ers.push_back(x);
	}
	return count_common_roads(ers);
}


bool check(vector<int>ch){
	ds.clear();
	int ent=0;
	set<int>ers;
	for(auto x:ch){
		ers.insert(x);
		ds.con(alle[x].first,alle[x].second);
	}
	for(auto x:adjt){
		if(ds.con(alle[x].first,alle[x].second)){
			ent+=mp[x];
			ers.insert(x);
		}
	}
	if(ent==pors(ers)){
		return 0;
	}
	return 1;
}

void upd(int ind){
	set<int>st;
	for(auto x:adjt){
		st.insert(x);
	}
	st.insert(ind);
	vector<int>alli;
	alli.push_back(ind);
	int mx=-1;
	int u=alle[ind].first,v=alle[ind].second;
	if(high[u]>high[v]){
		swap(u,v);
	}
	int cnt=-1;
	while(v!=u){
		alli.push_back(lnk[make_pair(v,p[v])]);
		v=p[v];
	}
	for(auto x:alli){
		if(mp.count(x)==1){
			cnt=x;
		}
	}
	if(cnt>=0){
		st.erase(cnt);
		int wtf=pors(st);
		st.insert(cnt);
		for(auto x:alli){
			if(mp.count(x)==0){
				st.erase(x);
				int tof=pors(st);
				st.insert(x);
				if(tof==wtf){
					mp[x]=mp[cnt];
				}else{
					mp[x]=mp[cnt]^1;
				}
				if(mp[x]){
					res.push_back(x);
				}
			}
		}
		return ;
	}
	for(auto x:alli){
		st.erase(x);
		mx=max(mx,pors(st));
		st.insert(x);
	}
	for(auto x:alli){
		st.erase(x);
		if(mx==pors(st)){
			mp[x]=0;
		}else{
			mp[x]=1;
			res.push_back(x);
		}
		st.insert(x);
	}
}

pair<int,int>buildt(int u=0,int par=-1){
	vis[u]=1;
	p[u]=par;
	pair<int,int>ret=make_pair(high[u],m);
	for(auto x:adj[u]){
		if(vis[x]==0){
			high[x]=high[u]+1;
			ret=min(ret,buildt(x,u));
			adjt.push_back(lnk[make_pair(u,x)]);
		}
	}
	for(auto x:adj[u]){
		if(x!=par){
		   ret=min(ret,make_pair(high[x],lnk[make_pair(u,x)]));
		}
	}
	if(u!=0){
		if(mp.count(lnk[make_pair(u,par)])==1){
			return ret;
		}
		if(ret.first>=high[u]){
			mp[lnk[make_pair(u,par)]]=1;
			res.push_back(lnk[make_pair(u,par)]);
		}else{
			updy.push_back(ret.second);
		}
	}
	return ret;
}

void erasea(){
	sort(adjt.begin(),adjt.end());
	set<int>sth;
	for(auto x:adjt){
		if(mp.count(x)==0){
			exit(23);
		}
		sth.insert(x);
	}
	for(int i=0;i<n;i++){
		vector<int>fake;
		for(auto x:adj[i]){
			if(mp.count(lnk[make_pair(i,x)])==0&&i<x){
				fake.push_back(x);
			}
		}
		swap(adj[i],fake);
	}
}

void bagh(){
	for(int i=0;i<n;i++){
		int low=0;
		while(low<(int)adj[i].size()){
			vector<int>heya;
			for(int j=low;j<(int)adj[i].size();j++){
				heya.push_back(lnk[make_pair(adj[i][j],i)]);
			}
			if(check(heya)==0){
				break;
			}
			int high=(int)adj[i].size(),mid;
			while(high-low>=1){
				mid=(high+low)>>1;
				vector<int>ch;
				for(int j=low;j<=mid;j++){
					ch.push_back(lnk[make_pair(i,adj[i][j])]);
				}
				if(check(ch)){
					high=mid;
				}else{
					low=mid+1;
				}
			}
			if(high!=(int)adj[i].size()){
				res.push_back(lnk[make_pair(i,adj[i][high])]);
				low=high+1;
			}
		}
	}
}

vector<int>solve(){
	buildt();
	for(auto x:res){
		//cout<<"magemishe "<<x<<endl;
	}
	for(auto x:updy){
		upd(x);
	}
	for(auto x:adjt){
		//cout<<"ha: "<<x<<endl;
	}
	for(auto x:res){
		//cout<<"magemishe "<<x<<endl;
	}
	erasea();
	bagh();
	return res;
}

std::vector<int> find_roads(int n_, std::vector<int> u, std::vector<int> v){
	m=(int)u.size();
	n=n_;
	for(int i=0;i<m;i++){
		lnk[make_pair(u[i],v[i])]=lnk[make_pair(v[i],u[i])]=i;
		adj[u[i]].push_back(v[i]);
		adj[v[i]].push_back(u[i]);
		alle[i]=make_pair(u[i],v[i]);
	}
	return solve();
}

Compilation message

simurgh.cpp: In function 'std::vector<int> solve()':
simurgh.cpp:220:11: warning: unused variable 'x' [-Wunused-variable]
  220 |  for(auto x:res){
      |           ^
simurgh.cpp:226:11: warning: unused variable 'x' [-Wunused-variable]
  226 |  for(auto x:adjt){
      |           ^
simurgh.cpp:229:11: warning: unused variable 'x' [-Wunused-variable]
  229 |  for(auto x:res){
      |           ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 0 ms 348 KB correct
4 Correct 1 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 348 KB correct
7 Correct 1 ms 348 KB correct
8 Correct 1 ms 344 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 1 ms 348 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 0 ms 348 KB correct
4 Correct 1 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 348 KB correct
7 Correct 1 ms 348 KB correct
8 Correct 1 ms 344 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 1 ms 348 KB correct
14 Correct 4 ms 604 KB correct
15 Correct 4 ms 604 KB correct
16 Correct 4 ms 604 KB correct
17 Correct 3 ms 604 KB correct
18 Correct 3 ms 344 KB correct
19 Correct 3 ms 600 KB correct
20 Correct 3 ms 604 KB correct
21 Correct 4 ms 600 KB correct
22 Correct 2 ms 348 KB correct
23 Correct 2 ms 348 KB correct
24 Correct 2 ms 348 KB correct
25 Correct 1 ms 360 KB correct
26 Correct 2 ms 348 KB correct
27 Correct 2 ms 348 KB correct
28 Correct 1 ms 348 KB correct
29 Correct 1 ms 344 KB correct
30 Correct 2 ms 348 KB correct
31 Correct 2 ms 348 KB correct
32 Correct 2 ms 348 KB correct
33 Correct 2 ms 348 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 0 ms 348 KB correct
4 Correct 1 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 348 KB correct
7 Correct 1 ms 348 KB correct
8 Correct 1 ms 344 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 1 ms 348 KB correct
14 Correct 4 ms 604 KB correct
15 Correct 4 ms 604 KB correct
16 Correct 4 ms 604 KB correct
17 Correct 3 ms 604 KB correct
18 Correct 3 ms 344 KB correct
19 Correct 3 ms 600 KB correct
20 Correct 3 ms 604 KB correct
21 Correct 4 ms 600 KB correct
22 Correct 2 ms 348 KB correct
23 Correct 2 ms 348 KB correct
24 Correct 2 ms 348 KB correct
25 Correct 1 ms 360 KB correct
26 Correct 2 ms 348 KB correct
27 Correct 2 ms 348 KB correct
28 Correct 1 ms 348 KB correct
29 Correct 1 ms 344 KB correct
30 Correct 2 ms 348 KB correct
31 Correct 2 ms 348 KB correct
32 Correct 2 ms 348 KB correct
33 Correct 2 ms 348 KB correct
34 Correct 120 ms 4956 KB correct
35 Correct 117 ms 4696 KB correct
36 Correct 102 ms 3600 KB correct
37 Correct 35 ms 600 KB correct
38 Correct 138 ms 4952 KB correct
39 Correct 121 ms 4388 KB correct
40 Correct 95 ms 3600 KB correct
41 Correct 114 ms 4940 KB correct
42 Correct 116 ms 4956 KB correct
43 Correct 57 ms 2904 KB correct
44 Correct 53 ms 2396 KB correct
45 Correct 55 ms 2764 KB correct
46 Correct 52 ms 2232 KB correct
47 Correct 36 ms 1112 KB correct
48 Correct 9 ms 348 KB correct
49 Correct 23 ms 752 KB correct
50 Correct 36 ms 1116 KB correct
51 Correct 65 ms 2764 KB correct
52 Correct 53 ms 2440 KB correct
53 Correct 52 ms 2140 KB correct
54 Correct 62 ms 2904 KB correct
55 Correct 52 ms 2792 KB correct
56 Correct 51 ms 2648 KB correct
57 Correct 52 ms 2652 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB correct
2 Correct 1 ms 344 KB correct
3 Correct 448 ms 13164 KB correct
4 Correct 722 ms 20048 KB correct
5 Correct 766 ms 20228 KB correct
6 Correct 693 ms 20052 KB correct
7 Correct 768 ms 20224 KB correct
8 Correct 703 ms 20228 KB correct
9 Correct 711 ms 20052 KB correct
10 Correct 731 ms 20304 KB correct
11 Correct 742 ms 20348 KB correct
12 Correct 752 ms 20228 KB correct
13 Correct 0 ms 600 KB correct
14 Correct 722 ms 20152 KB correct
15 Correct 714 ms 20224 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 0 ms 348 KB correct
4 Correct 1 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 348 KB correct
7 Correct 1 ms 348 KB correct
8 Correct 1 ms 344 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 1 ms 348 KB correct
14 Correct 4 ms 604 KB correct
15 Correct 4 ms 604 KB correct
16 Correct 4 ms 604 KB correct
17 Correct 3 ms 604 KB correct
18 Correct 3 ms 344 KB correct
19 Correct 3 ms 600 KB correct
20 Correct 3 ms 604 KB correct
21 Correct 4 ms 600 KB correct
22 Correct 2 ms 348 KB correct
23 Correct 2 ms 348 KB correct
24 Correct 2 ms 348 KB correct
25 Correct 1 ms 360 KB correct
26 Correct 2 ms 348 KB correct
27 Correct 2 ms 348 KB correct
28 Correct 1 ms 348 KB correct
29 Correct 1 ms 344 KB correct
30 Correct 2 ms 348 KB correct
31 Correct 2 ms 348 KB correct
32 Correct 2 ms 348 KB correct
33 Correct 2 ms 348 KB correct
34 Correct 120 ms 4956 KB correct
35 Correct 117 ms 4696 KB correct
36 Correct 102 ms 3600 KB correct
37 Correct 35 ms 600 KB correct
38 Correct 138 ms 4952 KB correct
39 Correct 121 ms 4388 KB correct
40 Correct 95 ms 3600 KB correct
41 Correct 114 ms 4940 KB correct
42 Correct 116 ms 4956 KB correct
43 Correct 57 ms 2904 KB correct
44 Correct 53 ms 2396 KB correct
45 Correct 55 ms 2764 KB correct
46 Correct 52 ms 2232 KB correct
47 Correct 36 ms 1112 KB correct
48 Correct 9 ms 348 KB correct
49 Correct 23 ms 752 KB correct
50 Correct 36 ms 1116 KB correct
51 Correct 65 ms 2764 KB correct
52 Correct 53 ms 2440 KB correct
53 Correct 52 ms 2140 KB correct
54 Correct 62 ms 2904 KB correct
55 Correct 52 ms 2792 KB correct
56 Correct 51 ms 2648 KB correct
57 Correct 52 ms 2652 KB correct
58 Correct 1 ms 344 KB correct
59 Correct 1 ms 344 KB correct
60 Correct 448 ms 13164 KB correct
61 Correct 722 ms 20048 KB correct
62 Correct 766 ms 20228 KB correct
63 Correct 693 ms 20052 KB correct
64 Correct 768 ms 20224 KB correct
65 Correct 703 ms 20228 KB correct
66 Correct 711 ms 20052 KB correct
67 Correct 731 ms 20304 KB correct
68 Correct 742 ms 20348 KB correct
69 Correct 752 ms 20228 KB correct
70 Correct 0 ms 600 KB correct
71 Correct 722 ms 20152 KB correct
72 Correct 714 ms 20224 KB correct
73 Correct 0 ms 344 KB correct
74 Correct 806 ms 20204 KB correct
75 Correct 699 ms 20440 KB correct
76 Correct 328 ms 7776 KB correct
77 Correct 760 ms 21008 KB correct
78 Correct 740 ms 21072 KB correct
79 Correct 748 ms 20892 KB correct
80 Correct 731 ms 20444 KB correct
81 Correct 647 ms 17492 KB correct
82 Correct 710 ms 20408 KB correct
83 Correct 544 ms 10904 KB correct
84 Correct 358 ms 12792 KB correct
85 Correct 333 ms 11544 KB correct
86 Correct 262 ms 8020 KB correct
87 Correct 249 ms 5720 KB correct
88 Correct 204 ms 4712 KB correct
89 Correct 219 ms 4488 KB correct
90 Correct 208 ms 3672 KB correct
91 Correct 94 ms 956 KB correct
92 Correct 39 ms 696 KB correct
93 Correct 331 ms 11480 KB correct
94 Correct 261 ms 8016 KB correct
95 Correct 284 ms 7604 KB correct
96 Correct 279 ms 4076 KB correct
97 Correct 215 ms 4688 KB correct
98 Correct 239 ms 5720 KB correct
99 Correct 210 ms 4872 KB correct
100 Correct 147 ms 1372 KB correct
101 Correct 57 ms 604 KB correct
102 Correct 282 ms 10700 KB correct
103 Correct 356 ms 10924 KB correct
104 Correct 277 ms 10576 KB correct
105 Correct 284 ms 10960 KB correct