# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1069338 |
2024-08-21T19:44:04 Z |
Ahmed57 |
Simurgh (IOI17_simurgh) |
C++17 |
|
116 ms |
34744 KB |
#include "bits/stdc++.h"
#include "simurgh.h"
using namespace std;
#pragma GCC optimize("Ofast")
int vis[300001];
int dep[300001];
pair<int,int> mi[300001];
pair<int,int> PR[300001];
vector<pair<int,int>> adj[300001];
vector<pair<int,int>> back[300001];
vector<pair<int,int>> tree[300001];
vector<int> tr;
vector<pair<int,int>> edg;
void dfs(int i,int pr){
vis[i] = 1;
dep[i] = dep[pr]+1;
mi[i] = {-1,-1};
for(auto j:adj[i]){
if(vis[j.first]&&dep[j.first]<dep[i]&&j.first!=pr){
if(mi[i].first==-1)mi[i] = {i,j.first};
else if(dep[j.first]<dep[mi[i].second])mi[i] = {i,j.first};
back[i].push_back(j);
//cout<<"back:"<<i<<" "<<j.first<<endl;
}
if(vis[j.first])continue;
PR[j.first] = {i,j.second};
tree[i].push_back(j);
//cout<<"tree:"<<i<<" "<<j.first<<" "<<j.second<<endl;
dfs(j.first,i);
tr.push_back(j.second);
if(mi[i].first==-1)mi[i] = mi[j.first];
else if(mi[j.first].first!=-1&&dep[mi[j.first].second]<dep[mi[i].second])mi[i] = mi[j.first];
}
}
int p[300001],N;
int ans[300001];
int find(int x){
if(x==p[x])return x;
return p[x] = find(p[x]);
}
void mergegroup(int a,int b){
a = find(a);b = find(b);
if(a==b)return ;
p[a] = b;
}
int que(vector<int> v,bool ss){
for(int i = 0;i<N;i++){
p[i] = i;
}
for(int i = 0;i<v.size();i++){
mergegroup(edg[v[i]].first,edg[v[i]].second);
}
int all = 0;
for(auto i:tr){
if(find(edg[i].first)!=find(edg[i].second)){
v.push_back(i);
all+=ans[i];
mergegroup(edg[i].first,edg[i].second);
}
}
if(ss)all = count_common_roads(v)-all;
else all = count_common_roads(v);
return all;
}
void answer(int i){
for(auto j:tree[i]){
if(ans[j.second]!=-1)continue;
//cout<<j.second<<endl;
int lol = mi[j.first].second;
if(lol==-1||dep[lol]>dep[i]){
ans[j.second] = 1;
}else{
int st = mi[j.first].first;
//cout<<st<<" "<<lol<<endl;
vector<int> v;
while(st!=lol){
v.push_back(PR[st].second);
st = PR[st].first;
}
for(auto e:back[mi[j.first].first]){
if(e.first==lol){
v.push_back(e.second);
break;
}
}
//for(auto e:v)cout<<e<<"xx ";
//cout<<endl;
int ma = -1;
for(auto e:v){
if(ans[e]==0){
vector<int> Q;
for(int w:v){
if(w==e)continue;
Q.push_back(w);
}
ma = que(Q,0);
break;
}
}
//cout<<"ddd"<<endl;
if(ma!=-1){
//cout<<j.second<<"lollll"<<endl;
for(auto e:v){
if(ans[e]==-1){
vector<int> Q;
for(int w:v){
if(w==e)continue;
Q.push_back(w);
}
if(ma==que(Q,0)){
ans[e] = 0;
}else ans[e] = 1;
}
}
}else{
set<pair<int ,int >> s;
//cout<<"dd"<<endl;
for(auto e:v){
if(ans[e]==-1){
vector<int> Q;
for(int w:v){
if(w==e)continue;
Q.push_back(w);
}
s.insert({que(Q,0),e});
}
}
int la = (*(--s.end())).first;
int sm = (*s.begin()).first;
if(la==sm){
for(auto w:s)ans[w.second] = 0;
}else{
for(auto w:s){
//cout<<w.first<<"nanan"<<w.second<<endl;
if(w.first==la)ans[w.second] = 0;
else ans[w.second] = 1;
//cout<<w.second<<" "<<ans[w.second]<<endl;
}
}
}
}
}
for(auto j:tree[i]){answer(j.first);}
}
vector<int> find_roads(int n, vector<int> u, vector<int> v){
N = n;
//cout<<"hhh"<<endl;
for(int i = 0;i<u.size();i++){
ans[i] = -1;
adj[u[i]].push_back({v[i],i});
adj[v[i]].push_back({u[i],i});
edg.push_back({u[i],v[i]});
}
PR[0] = {-1,-1};
dfs(0,0);
//cout<<"hh"<<endl;
answer(0);
//cout<<"vvv"<<endl;
for(int i = 0;i<n;i++){
int la = 0;
while(1){
if(la==back[i].size())break;
vector<int> Q;
for(int e = la;e<back[i].size();e++)Q.push_back(back[i][e].second);
if(que(Q,1)){
int l = la , r = back[i].size()-1 , ANS = 0;
while(l<=r){
int mid = (l+r)/2;
vector<int> Q;
for(int e = la;e<=mid;e++){
Q.push_back(back[i][e].second);
}
if(que(Q,1)){
ANS = mid;r = mid-1;
}else l = mid+1;
}
ans[back[i][ANS].second] = 1;
la = ANS+1;
}else break;
}
}
vector<int> good;
for(int i = 0;i<u.size();i++){
if(ans[i]==1){
good.push_back(i);
}
}
//cout<<"ggg"<<endl;
/*for(int i = 0;i<good.size();i++){
cout<<good[i]<<"hhhh";
}*/
return good;
}
Compilation message
simurgh.cpp: In function 'int que(std::vector<int>, bool)':
simurgh.cpp:51:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for(int i = 0;i<v.size();i++){
| ~^~~~~~~~~
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:150:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
150 | for(int i = 0;i<u.size();i++){
| ~^~~~~~~~~
simurgh.cpp:164:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
164 | if(la==back[i].size())break;
| ~~^~~~~~~~~~~~~~~~
simurgh.cpp:166:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
166 | for(int e = la;e<back[i].size();e++)Q.push_back(back[i][e].second);
| ~^~~~~~~~~~~~~~~
simurgh.cpp:185:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
185 | for(int i = 0;i<u.size();i++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
26972 KB |
correct |
2 |
Correct |
4 ms |
26972 KB |
correct |
3 |
Correct |
5 ms |
26968 KB |
correct |
4 |
Correct |
4 ms |
26972 KB |
correct |
5 |
Correct |
4 ms |
26972 KB |
correct |
6 |
Correct |
4 ms |
27132 KB |
correct |
7 |
Correct |
4 ms |
26968 KB |
correct |
8 |
Correct |
4 ms |
26972 KB |
correct |
9 |
Correct |
4 ms |
26972 KB |
correct |
10 |
Correct |
4 ms |
26972 KB |
correct |
11 |
Correct |
4 ms |
26972 KB |
correct |
12 |
Correct |
4 ms |
26972 KB |
correct |
13 |
Correct |
4 ms |
26968 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
26972 KB |
correct |
2 |
Correct |
4 ms |
26972 KB |
correct |
3 |
Correct |
5 ms |
26968 KB |
correct |
4 |
Correct |
4 ms |
26972 KB |
correct |
5 |
Correct |
4 ms |
26972 KB |
correct |
6 |
Correct |
4 ms |
27132 KB |
correct |
7 |
Correct |
4 ms |
26968 KB |
correct |
8 |
Correct |
4 ms |
26972 KB |
correct |
9 |
Correct |
4 ms |
26972 KB |
correct |
10 |
Correct |
4 ms |
26972 KB |
correct |
11 |
Correct |
4 ms |
26972 KB |
correct |
12 |
Correct |
4 ms |
26972 KB |
correct |
13 |
Correct |
4 ms |
26968 KB |
correct |
14 |
Correct |
5 ms |
27228 KB |
correct |
15 |
Correct |
5 ms |
27228 KB |
correct |
16 |
Correct |
5 ms |
27228 KB |
correct |
17 |
Correct |
5 ms |
26972 KB |
correct |
18 |
Correct |
4 ms |
26968 KB |
correct |
19 |
Correct |
5 ms |
26972 KB |
correct |
20 |
Correct |
5 ms |
26972 KB |
correct |
21 |
Correct |
5 ms |
27216 KB |
correct |
22 |
Correct |
4 ms |
26972 KB |
correct |
23 |
Correct |
4 ms |
27184 KB |
correct |
24 |
Correct |
4 ms |
27088 KB |
correct |
25 |
Correct |
4 ms |
26972 KB |
correct |
26 |
Correct |
4 ms |
26972 KB |
correct |
27 |
Correct |
4 ms |
26972 KB |
correct |
28 |
Correct |
5 ms |
26972 KB |
correct |
29 |
Correct |
4 ms |
26972 KB |
correct |
30 |
Correct |
4 ms |
26972 KB |
correct |
31 |
Correct |
4 ms |
26968 KB |
correct |
32 |
Correct |
4 ms |
26972 KB |
correct |
33 |
Correct |
5 ms |
26972 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
26972 KB |
correct |
2 |
Correct |
4 ms |
26972 KB |
correct |
3 |
Correct |
5 ms |
26968 KB |
correct |
4 |
Correct |
4 ms |
26972 KB |
correct |
5 |
Correct |
4 ms |
26972 KB |
correct |
6 |
Correct |
4 ms |
27132 KB |
correct |
7 |
Correct |
4 ms |
26968 KB |
correct |
8 |
Correct |
4 ms |
26972 KB |
correct |
9 |
Correct |
4 ms |
26972 KB |
correct |
10 |
Correct |
4 ms |
26972 KB |
correct |
11 |
Correct |
4 ms |
26972 KB |
correct |
12 |
Correct |
4 ms |
26972 KB |
correct |
13 |
Correct |
4 ms |
26968 KB |
correct |
14 |
Correct |
5 ms |
27228 KB |
correct |
15 |
Correct |
5 ms |
27228 KB |
correct |
16 |
Correct |
5 ms |
27228 KB |
correct |
17 |
Correct |
5 ms |
26972 KB |
correct |
18 |
Correct |
4 ms |
26968 KB |
correct |
19 |
Correct |
5 ms |
26972 KB |
correct |
20 |
Correct |
5 ms |
26972 KB |
correct |
21 |
Correct |
5 ms |
27216 KB |
correct |
22 |
Correct |
4 ms |
26972 KB |
correct |
23 |
Correct |
4 ms |
27184 KB |
correct |
24 |
Correct |
4 ms |
27088 KB |
correct |
25 |
Correct |
4 ms |
26972 KB |
correct |
26 |
Correct |
4 ms |
26972 KB |
correct |
27 |
Correct |
4 ms |
26972 KB |
correct |
28 |
Correct |
5 ms |
26972 KB |
correct |
29 |
Correct |
4 ms |
26972 KB |
correct |
30 |
Correct |
4 ms |
26972 KB |
correct |
31 |
Correct |
4 ms |
26968 KB |
correct |
32 |
Correct |
4 ms |
26972 KB |
correct |
33 |
Correct |
5 ms |
26972 KB |
correct |
34 |
Correct |
25 ms |
28632 KB |
correct |
35 |
Correct |
25 ms |
28632 KB |
correct |
36 |
Correct |
22 ms |
28372 KB |
correct |
37 |
Correct |
10 ms |
27228 KB |
correct |
38 |
Correct |
25 ms |
28632 KB |
correct |
39 |
Correct |
22 ms |
28632 KB |
correct |
40 |
Correct |
21 ms |
28372 KB |
correct |
41 |
Correct |
21 ms |
28640 KB |
correct |
42 |
Correct |
24 ms |
28632 KB |
correct |
43 |
Correct |
12 ms |
27996 KB |
correct |
44 |
Correct |
12 ms |
27736 KB |
correct |
45 |
Correct |
12 ms |
27864 KB |
correct |
46 |
Correct |
12 ms |
27740 KB |
correct |
47 |
Correct |
9 ms |
27484 KB |
correct |
48 |
Correct |
6 ms |
26972 KB |
correct |
49 |
Correct |
8 ms |
27228 KB |
correct |
50 |
Correct |
10 ms |
27524 KB |
correct |
51 |
Correct |
14 ms |
27996 KB |
correct |
52 |
Correct |
12 ms |
27736 KB |
correct |
53 |
Correct |
16 ms |
27736 KB |
correct |
54 |
Correct |
12 ms |
27992 KB |
correct |
55 |
Correct |
15 ms |
28196 KB |
correct |
56 |
Correct |
12 ms |
28000 KB |
correct |
57 |
Correct |
13 ms |
27956 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
27116 KB |
correct |
2 |
Correct |
4 ms |
26972 KB |
correct |
3 |
Correct |
64 ms |
31704 KB |
correct |
4 |
Correct |
97 ms |
33736 KB |
correct |
5 |
Correct |
101 ms |
34728 KB |
correct |
6 |
Correct |
98 ms |
34744 KB |
correct |
7 |
Correct |
89 ms |
34508 KB |
correct |
8 |
Correct |
91 ms |
34512 KB |
correct |
9 |
Correct |
96 ms |
34740 KB |
correct |
10 |
Correct |
104 ms |
34692 KB |
correct |
11 |
Correct |
99 ms |
34508 KB |
correct |
12 |
Correct |
101 ms |
34496 KB |
correct |
13 |
Correct |
4 ms |
26968 KB |
correct |
14 |
Correct |
116 ms |
34616 KB |
correct |
15 |
Correct |
100 ms |
34512 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
26972 KB |
correct |
2 |
Correct |
4 ms |
26972 KB |
correct |
3 |
Correct |
5 ms |
26968 KB |
correct |
4 |
Correct |
4 ms |
26972 KB |
correct |
5 |
Correct |
4 ms |
26972 KB |
correct |
6 |
Correct |
4 ms |
27132 KB |
correct |
7 |
Correct |
4 ms |
26968 KB |
correct |
8 |
Correct |
4 ms |
26972 KB |
correct |
9 |
Correct |
4 ms |
26972 KB |
correct |
10 |
Correct |
4 ms |
26972 KB |
correct |
11 |
Correct |
4 ms |
26972 KB |
correct |
12 |
Correct |
4 ms |
26972 KB |
correct |
13 |
Correct |
4 ms |
26968 KB |
correct |
14 |
Correct |
5 ms |
27228 KB |
correct |
15 |
Correct |
5 ms |
27228 KB |
correct |
16 |
Correct |
5 ms |
27228 KB |
correct |
17 |
Correct |
5 ms |
26972 KB |
correct |
18 |
Correct |
4 ms |
26968 KB |
correct |
19 |
Correct |
5 ms |
26972 KB |
correct |
20 |
Correct |
5 ms |
26972 KB |
correct |
21 |
Correct |
5 ms |
27216 KB |
correct |
22 |
Correct |
4 ms |
26972 KB |
correct |
23 |
Correct |
4 ms |
27184 KB |
correct |
24 |
Correct |
4 ms |
27088 KB |
correct |
25 |
Correct |
4 ms |
26972 KB |
correct |
26 |
Correct |
4 ms |
26972 KB |
correct |
27 |
Correct |
4 ms |
26972 KB |
correct |
28 |
Correct |
5 ms |
26972 KB |
correct |
29 |
Correct |
4 ms |
26972 KB |
correct |
30 |
Correct |
4 ms |
26972 KB |
correct |
31 |
Correct |
4 ms |
26968 KB |
correct |
32 |
Correct |
4 ms |
26972 KB |
correct |
33 |
Correct |
5 ms |
26972 KB |
correct |
34 |
Correct |
25 ms |
28632 KB |
correct |
35 |
Correct |
25 ms |
28632 KB |
correct |
36 |
Correct |
22 ms |
28372 KB |
correct |
37 |
Correct |
10 ms |
27228 KB |
correct |
38 |
Correct |
25 ms |
28632 KB |
correct |
39 |
Correct |
22 ms |
28632 KB |
correct |
40 |
Correct |
21 ms |
28372 KB |
correct |
41 |
Correct |
21 ms |
28640 KB |
correct |
42 |
Correct |
24 ms |
28632 KB |
correct |
43 |
Correct |
12 ms |
27996 KB |
correct |
44 |
Correct |
12 ms |
27736 KB |
correct |
45 |
Correct |
12 ms |
27864 KB |
correct |
46 |
Correct |
12 ms |
27740 KB |
correct |
47 |
Correct |
9 ms |
27484 KB |
correct |
48 |
Correct |
6 ms |
26972 KB |
correct |
49 |
Correct |
8 ms |
27228 KB |
correct |
50 |
Correct |
10 ms |
27524 KB |
correct |
51 |
Correct |
14 ms |
27996 KB |
correct |
52 |
Correct |
12 ms |
27736 KB |
correct |
53 |
Correct |
16 ms |
27736 KB |
correct |
54 |
Correct |
12 ms |
27992 KB |
correct |
55 |
Correct |
15 ms |
28196 KB |
correct |
56 |
Correct |
12 ms |
28000 KB |
correct |
57 |
Correct |
13 ms |
27956 KB |
correct |
58 |
Correct |
4 ms |
27116 KB |
correct |
59 |
Correct |
4 ms |
26972 KB |
correct |
60 |
Correct |
64 ms |
31704 KB |
correct |
61 |
Correct |
97 ms |
33736 KB |
correct |
62 |
Correct |
101 ms |
34728 KB |
correct |
63 |
Correct |
98 ms |
34744 KB |
correct |
64 |
Correct |
89 ms |
34508 KB |
correct |
65 |
Correct |
91 ms |
34512 KB |
correct |
66 |
Correct |
96 ms |
34740 KB |
correct |
67 |
Correct |
104 ms |
34692 KB |
correct |
68 |
Correct |
99 ms |
34508 KB |
correct |
69 |
Correct |
101 ms |
34496 KB |
correct |
70 |
Correct |
4 ms |
26968 KB |
correct |
71 |
Correct |
116 ms |
34616 KB |
correct |
72 |
Correct |
100 ms |
34512 KB |
correct |
73 |
Correct |
4 ms |
26968 KB |
correct |
74 |
Correct |
103 ms |
34744 KB |
correct |
75 |
Correct |
97 ms |
34576 KB |
correct |
76 |
Correct |
54 ms |
30160 KB |
correct |
77 |
Correct |
101 ms |
34508 KB |
correct |
78 |
Correct |
105 ms |
34620 KB |
correct |
79 |
Correct |
101 ms |
34504 KB |
correct |
80 |
Correct |
99 ms |
34500 KB |
correct |
81 |
Correct |
89 ms |
33872 KB |
correct |
82 |
Correct |
111 ms |
34552 KB |
correct |
83 |
Correct |
77 ms |
31184 KB |
correct |
84 |
Correct |
47 ms |
32204 KB |
correct |
85 |
Correct |
45 ms |
31700 KB |
correct |
86 |
Correct |
38 ms |
30388 KB |
correct |
87 |
Correct |
35 ms |
29400 KB |
correct |
88 |
Correct |
31 ms |
28884 KB |
correct |
89 |
Correct |
32 ms |
28888 KB |
correct |
90 |
Correct |
29 ms |
28628 KB |
correct |
91 |
Correct |
18 ms |
27224 KB |
correct |
92 |
Correct |
13 ms |
27228 KB |
correct |
93 |
Correct |
43 ms |
31700 KB |
correct |
94 |
Correct |
38 ms |
30160 KB |
correct |
95 |
Correct |
42 ms |
30160 KB |
correct |
96 |
Correct |
32 ms |
28628 KB |
correct |
97 |
Correct |
38 ms |
28888 KB |
correct |
98 |
Correct |
34 ms |
29396 KB |
correct |
99 |
Correct |
32 ms |
28888 KB |
correct |
100 |
Correct |
23 ms |
27480 KB |
correct |
101 |
Correct |
19 ms |
27284 KB |
correct |
102 |
Correct |
43 ms |
30928 KB |
correct |
103 |
Correct |
43 ms |
30932 KB |
correct |
104 |
Correct |
43 ms |
30932 KB |
correct |
105 |
Correct |
44 ms |
30928 KB |
correct |