#include "simurgh.h"
#include <bits/stdc++.h>
#define N 500
using namespace std;
set<int> adj[N];
int edge[N][N];
int par[N];
bool can[N*N];
int in_answer[N*N];
int find(int a){
if(par[a] == a)return a;
return par[a] = find(par[a]);
}
bool merge(int a,int b){
a = find(a);
b = find(b);
if(a == b)
return 0;
par[b] = a;
return 1;
}
vector<int> edges;
int dfspar[N];
void dfs(int v,int target,int pr){
//cout << v << " " << target << " " << pr << endl;
dfspar[v] = pr;
if(v == target){
while(dfspar[v] != -1){
edges.push_back(edge[v][dfspar[v]]);
v = dfspar[v];
}
return;
}
for(auto u:adj[v]){
if(u == pr)continue;
dfs(u,target,v);
}
}
map<vector<int>,int> mp;
int get(set<int> s){
vector<int> r;
for(auto u:s)r.push_back(u);
if(mp.count(r))
return mp[r];
return mp[r] = count_common_roads(r);
}
vector<int> find_roads(int n, vector<int> u, vector<int> v) {
int m = u.size();
for(int i = 0;i<n;i++){
for(int j = 0;j<n;j++){
edge[i][j] = -1;
}
par[i] = i;
}
for(int i = 0;i<m;i++){
can[i] = 1;
in_answer[i] = -1;
edge[u[i]][v[i]] = i;
edge[v[i]][u[i]] = i;
}
set<int> r;
//cout << endl;
for(int i = 0;i<m;i++){
//cout << u[i] << " " << v[i] << endl;
if(merge(u[i],v[i])){
//cout << i << endl;
can[i] = 0;
adj[u[i]].insert(v[i]);
adj[v[i]].insert(u[i]);
r.insert(i);
}
}
// cout << "hey" << endl;
// for(auto u:r){
// cout << u << " ";
// }
// cout << endl;
int last = 0;
while(get(r) != n-1){
while(!can[last])
last++;
//cout << "hey" << endl;
int best = get(r);
edges.clear();
//cout << "hey" << endl;
dfs(u[last],v[last],-1);
// cout << "hey" << endl;
// for(auto u:r){
// cout << u << " ";
// }
// cout << endl;
// for(auto x:edges){
// cout << x << " ";
// }
// cout << endl;
// cout << last << endl;
vector<int> known;
vector<int> unknown;
for(auto x:edges){
if(in_answer[x] == -1){
unknown.push_back(x);
}
else known.push_back(x);
}
bool add_cur = 0;
for(auto x:unknown){
r.erase(x);
r.insert(last);
if(get(r) > best){
add_cur = 1;
}
r.erase(last);
r.insert(x);
}
if(known.size()){
r.erase(known[0]);
r.insert(last);
if(get(r) > best - in_answer[known[0]]){
add_cur = 1;
}
r.erase(last);
r.insert(known[0]);
}
for(auto x:unknown){
r.erase(x);
r.insert(last);
if(best - (get(r) - add_cur) > 0){
in_answer[x] = 1;
}
else in_answer[x] = 0;
r.erase(last);
r.insert(x);
}
if(add_cur){
for(auto x:edges){
if(in_answer[x] == 0){
r.erase(x);
r.insert(last);
adj[u[x]].erase(v[x]);
adj[v[x]].erase(u[x]);
adj[u[last]].insert(v[last]);
adj[v[last]].insert(u[last]);
in_answer[last] = 1;
break;
}
}
}
last++;
}
vector<int> ret;
for(auto u:r){
ret.push_back(u);
}
return ret;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
correct |
2 |
Correct |
1 ms |
340 KB |
correct |
3 |
Correct |
1 ms |
340 KB |
correct |
4 |
Correct |
1 ms |
340 KB |
correct |
5 |
Correct |
1 ms |
340 KB |
correct |
6 |
Correct |
0 ms |
340 KB |
correct |
7 |
Correct |
0 ms |
340 KB |
correct |
8 |
Correct |
1 ms |
340 KB |
correct |
9 |
Correct |
1 ms |
340 KB |
correct |
10 |
Correct |
1 ms |
336 KB |
correct |
11 |
Correct |
1 ms |
340 KB |
correct |
12 |
Correct |
1 ms |
340 KB |
correct |
13 |
Correct |
1 ms |
332 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
correct |
2 |
Correct |
1 ms |
340 KB |
correct |
3 |
Correct |
1 ms |
340 KB |
correct |
4 |
Correct |
1 ms |
340 KB |
correct |
5 |
Correct |
1 ms |
340 KB |
correct |
6 |
Correct |
0 ms |
340 KB |
correct |
7 |
Correct |
0 ms |
340 KB |
correct |
8 |
Correct |
1 ms |
340 KB |
correct |
9 |
Correct |
1 ms |
340 KB |
correct |
10 |
Correct |
1 ms |
336 KB |
correct |
11 |
Correct |
1 ms |
340 KB |
correct |
12 |
Correct |
1 ms |
340 KB |
correct |
13 |
Correct |
1 ms |
332 KB |
correct |
14 |
Correct |
15 ms |
724 KB |
correct |
15 |
Correct |
14 ms |
724 KB |
correct |
16 |
Correct |
13 ms |
696 KB |
correct |
17 |
Correct |
12 ms |
728 KB |
correct |
18 |
Correct |
5 ms |
468 KB |
correct |
19 |
Correct |
14 ms |
708 KB |
correct |
20 |
Correct |
10 ms |
724 KB |
correct |
21 |
Correct |
9 ms |
676 KB |
correct |
22 |
Correct |
1 ms |
468 KB |
correct |
23 |
Correct |
1 ms |
340 KB |
correct |
24 |
Correct |
1 ms |
340 KB |
correct |
25 |
Correct |
1 ms |
340 KB |
correct |
26 |
Correct |
1 ms |
340 KB |
correct |
27 |
Correct |
1 ms |
468 KB |
correct |
28 |
Correct |
1 ms |
340 KB |
correct |
29 |
Correct |
1 ms |
340 KB |
correct |
30 |
Correct |
1 ms |
340 KB |
correct |
31 |
Correct |
1 ms |
340 KB |
correct |
32 |
Correct |
1 ms |
340 KB |
correct |
33 |
Correct |
1 ms |
340 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
correct |
2 |
Correct |
1 ms |
340 KB |
correct |
3 |
Correct |
1 ms |
340 KB |
correct |
4 |
Correct |
1 ms |
340 KB |
correct |
5 |
Correct |
1 ms |
340 KB |
correct |
6 |
Correct |
0 ms |
340 KB |
correct |
7 |
Correct |
0 ms |
340 KB |
correct |
8 |
Correct |
1 ms |
340 KB |
correct |
9 |
Correct |
1 ms |
340 KB |
correct |
10 |
Correct |
1 ms |
336 KB |
correct |
11 |
Correct |
1 ms |
340 KB |
correct |
12 |
Correct |
1 ms |
340 KB |
correct |
13 |
Correct |
1 ms |
332 KB |
correct |
14 |
Correct |
15 ms |
724 KB |
correct |
15 |
Correct |
14 ms |
724 KB |
correct |
16 |
Correct |
13 ms |
696 KB |
correct |
17 |
Correct |
12 ms |
728 KB |
correct |
18 |
Correct |
5 ms |
468 KB |
correct |
19 |
Correct |
14 ms |
708 KB |
correct |
20 |
Correct |
10 ms |
724 KB |
correct |
21 |
Correct |
9 ms |
676 KB |
correct |
22 |
Correct |
1 ms |
468 KB |
correct |
23 |
Correct |
1 ms |
340 KB |
correct |
24 |
Correct |
1 ms |
340 KB |
correct |
25 |
Correct |
1 ms |
340 KB |
correct |
26 |
Correct |
1 ms |
340 KB |
correct |
27 |
Correct |
1 ms |
468 KB |
correct |
28 |
Correct |
1 ms |
340 KB |
correct |
29 |
Correct |
1 ms |
340 KB |
correct |
30 |
Correct |
1 ms |
340 KB |
correct |
31 |
Correct |
1 ms |
340 KB |
correct |
32 |
Correct |
1 ms |
340 KB |
correct |
33 |
Correct |
1 ms |
340 KB |
correct |
34 |
Correct |
1370 ms |
31136 KB |
correct |
35 |
Correct |
1339 ms |
30192 KB |
correct |
36 |
Correct |
872 ms |
20912 KB |
correct |
37 |
Correct |
49 ms |
2120 KB |
correct |
38 |
Correct |
1344 ms |
30848 KB |
correct |
39 |
Correct |
1122 ms |
26512 KB |
correct |
40 |
Correct |
891 ms |
21036 KB |
correct |
41 |
Correct |
1316 ms |
31248 KB |
correct |
42 |
Correct |
1317 ms |
31124 KB |
correct |
43 |
Correct |
4 ms |
1236 KB |
correct |
44 |
Correct |
3 ms |
1108 KB |
correct |
45 |
Correct |
3 ms |
1236 KB |
correct |
46 |
Correct |
3 ms |
1108 KB |
correct |
47 |
Correct |
2 ms |
996 KB |
correct |
48 |
Correct |
1 ms |
852 KB |
correct |
49 |
Correct |
1 ms |
852 KB |
correct |
50 |
Correct |
2 ms |
980 KB |
correct |
51 |
Correct |
4 ms |
1244 KB |
correct |
52 |
Correct |
5 ms |
1108 KB |
correct |
53 |
Correct |
3 ms |
1108 KB |
correct |
54 |
Correct |
5 ms |
1248 KB |
correct |
55 |
Correct |
4 ms |
1236 KB |
correct |
56 |
Correct |
4 ms |
1236 KB |
correct |
57 |
Correct |
4 ms |
1184 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
correct |
2 |
Correct |
1 ms |
340 KB |
correct |
3 |
Incorrect |
1010 ms |
23472 KB |
WA in grader: NO |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
correct |
2 |
Correct |
1 ms |
340 KB |
correct |
3 |
Correct |
1 ms |
340 KB |
correct |
4 |
Correct |
1 ms |
340 KB |
correct |
5 |
Correct |
1 ms |
340 KB |
correct |
6 |
Correct |
0 ms |
340 KB |
correct |
7 |
Correct |
0 ms |
340 KB |
correct |
8 |
Correct |
1 ms |
340 KB |
correct |
9 |
Correct |
1 ms |
340 KB |
correct |
10 |
Correct |
1 ms |
336 KB |
correct |
11 |
Correct |
1 ms |
340 KB |
correct |
12 |
Correct |
1 ms |
340 KB |
correct |
13 |
Correct |
1 ms |
332 KB |
correct |
14 |
Correct |
15 ms |
724 KB |
correct |
15 |
Correct |
14 ms |
724 KB |
correct |
16 |
Correct |
13 ms |
696 KB |
correct |
17 |
Correct |
12 ms |
728 KB |
correct |
18 |
Correct |
5 ms |
468 KB |
correct |
19 |
Correct |
14 ms |
708 KB |
correct |
20 |
Correct |
10 ms |
724 KB |
correct |
21 |
Correct |
9 ms |
676 KB |
correct |
22 |
Correct |
1 ms |
468 KB |
correct |
23 |
Correct |
1 ms |
340 KB |
correct |
24 |
Correct |
1 ms |
340 KB |
correct |
25 |
Correct |
1 ms |
340 KB |
correct |
26 |
Correct |
1 ms |
340 KB |
correct |
27 |
Correct |
1 ms |
468 KB |
correct |
28 |
Correct |
1 ms |
340 KB |
correct |
29 |
Correct |
1 ms |
340 KB |
correct |
30 |
Correct |
1 ms |
340 KB |
correct |
31 |
Correct |
1 ms |
340 KB |
correct |
32 |
Correct |
1 ms |
340 KB |
correct |
33 |
Correct |
1 ms |
340 KB |
correct |
34 |
Correct |
1370 ms |
31136 KB |
correct |
35 |
Correct |
1339 ms |
30192 KB |
correct |
36 |
Correct |
872 ms |
20912 KB |
correct |
37 |
Correct |
49 ms |
2120 KB |
correct |
38 |
Correct |
1344 ms |
30848 KB |
correct |
39 |
Correct |
1122 ms |
26512 KB |
correct |
40 |
Correct |
891 ms |
21036 KB |
correct |
41 |
Correct |
1316 ms |
31248 KB |
correct |
42 |
Correct |
1317 ms |
31124 KB |
correct |
43 |
Correct |
4 ms |
1236 KB |
correct |
44 |
Correct |
3 ms |
1108 KB |
correct |
45 |
Correct |
3 ms |
1236 KB |
correct |
46 |
Correct |
3 ms |
1108 KB |
correct |
47 |
Correct |
2 ms |
996 KB |
correct |
48 |
Correct |
1 ms |
852 KB |
correct |
49 |
Correct |
1 ms |
852 KB |
correct |
50 |
Correct |
2 ms |
980 KB |
correct |
51 |
Correct |
4 ms |
1244 KB |
correct |
52 |
Correct |
5 ms |
1108 KB |
correct |
53 |
Correct |
3 ms |
1108 KB |
correct |
54 |
Correct |
5 ms |
1248 KB |
correct |
55 |
Correct |
4 ms |
1236 KB |
correct |
56 |
Correct |
4 ms |
1236 KB |
correct |
57 |
Correct |
4 ms |
1184 KB |
correct |
58 |
Correct |
0 ms |
340 KB |
correct |
59 |
Correct |
1 ms |
340 KB |
correct |
60 |
Incorrect |
1010 ms |
23472 KB |
WA in grader: NO |
61 |
Halted |
0 ms |
0 KB |
- |