#include "simurgh.h"
#include<vector>
#include<queue>
#include<utility>
#include<iostream>
#define MAXN 150005
using namespace std;
vector<pair<int,int> > adj[MAXN], Tree[MAXN]; //to, edge id
vector<int> TreeId, Ans;
int N,M,X, rt = 0; //spanning tree count
int low[MAXN], lowid[MAXN], par[MAXN], parid[MAXN], depth[MAXN], res[MAXN];
bool golden[MAXN], vis[MAXN], vis2[MAXN], done[MAXN];
//tree making phrase
void dfs(int u) { //make dfs tree
vis[u] = true;
for (auto v: adj[u]) {
if (!vis[v.first]) {
TreeId.push_back(v.second);
Tree[u].push_back(v);
Tree[v.first].push_back({u,v.second});
depth[v.first] = depth[u] + 1;
par[v.first] = u, parid[v.first] = v.second;
dfs(v.first);
}
}
}
void solveTree(int u) { //solve dfs tree's identity
vis2[u] = true, low[u] = u;
int chd_low = u;
for (auto v: adj[u]) {
if (vis2[v.first] && v.first != par[u]) {
if (depth[low[u]] > depth[v.first]) {
low[u] = v.first;
lowid[u] = v.second;
}
} else if (!vis2[v.first]) {
solveTree(v.first);
if (depth[chd_low] > depth[low[v.first]]) {
chd_low = low[v.first];
}
}
}
if (low[u] == u && chd_low == u && u != rt) {
golden[parid[u]] = true;
done[parid[u]] = true; //which edges u-par(u) have been solved?
} else if (depth[low[u]] >= depth[chd_low]) {
low[u] = chd_low; //relax
} else {
int cur = u, seen_normal = false, t1=false, t2=false, t3=false;
while (cur != low[u] && cur != rt) {
if (!done[parid[cur]] || (!seen_normal && !golden[parid[cur]])) {
//printf("Solving %d. Par=%d. Edge Id %d, low = %d non-tree=%d\n", cur, par[cur], parid[cur], low[u],lowid[u]);
vector<int> query;
query.push_back(lowid[u]);
for (int i=0; i<N-1; i++) {
if (TreeId[i] != parid[cur]) {
query.push_back(TreeId[i]);
}
}
res[cur] = count_common_roads(query);
if (res[cur] == X-1) {
t1 = true;
} else if (res[cur] == X) {
t2 = true;
} else if (res[cur] == X+1) {
t3 = true;
}
if (done[parid[cur]]) {seen_normal = true;}
}
cur = par[cur];
}
cur = u;
while (cur != low[u] && cur != rt) {
if (!done[parid[cur]]) {
done[parid[cur]] = true;
if (t1 && res[cur] == X-1) {
golden[parid[cur]] = true;
} else if (t3 && res[cur] == X) {
golden[parid[cur]] = true;
}
}
cur = par[cur];
}
done[lowid[u]] = true;
if (t3 == true) {
golden[lowid[u]] = true;
}
}
}
//Solving phrase
int Num(vector<pair<int,int> > Eset, int v) {
int C = Eset.size(), D = 0; //count golden in Eset, adj of v
bool vis3[MAXN] = {};
queue<int> Q;
vector<int> Query;
for (auto e: Eset) {
Tree[v].push_back(e);
}
Q.push(v), vis3[v] = true;
while (Q.size()) {
int cur = Q.front();
Q.pop();
for (int i=Tree[cur].size()-1; i>=0; i--) {
if (!vis3[Tree[cur][i].first]) {
vis3[Tree[cur][i].first] = true;
Q.push(Tree[cur][i].first);
Query.push_back(Tree[cur][i].second);
if (done[Tree[cur][i].second]) {
D += (int) golden[Tree[cur][i].second];
}
}
}
}
for (int i=0; i<C; i++) {
Tree[v].pop_back();
}
return(count_common_roads(Query) - D);
}
void FindGolden(int u) {
vector <pair<int,int> > todo;
for (auto v: adj[u]) {
if (!done[v.second]) {
todo.push_back(v); //all not done
}
}
int deg = Num(todo, u);
for (int i=0; i<deg; i++) {
vector <pair<int,int> > Maybe;
for (auto v: adj[u]) {
if (!done[v.second]) {
Maybe.push_back(v);
}
}
int lo = 0, hi = Maybe.size()-1;
while (lo != hi) {
int mid = (lo + hi) >> 1;
vector<pair<int,int> > query;
for (int j=lo; j<=mid; j++) {
query.push_back(Maybe[j]);
}
if (Num(query, u)) {
hi = mid;
} else {
lo = mid + 1;
}
}
golden[Maybe[hi].second] = true;
done[Maybe[hi].second] = true;
}
for (auto v: adj[u]) {
done[v.second] = true;
}
}
vector<int> find_roads(int n, vector<int> u, vector<int> v) {
N = n, M = u.size();
for (int i=0; i<M; i++) {
adj[u[i]].push_back({v[i],i});
adj[v[i]].push_back({u[i],i});
}
par[rt] = -1, dfs(rt);
X = count_common_roads(TreeId);
solveTree(rt);
for (int i=0; i<N; i++) {
FindGolden(i); //find all golden edges adj to i
}
for (int i=0; i<M; i++) {
if (golden[i]) {
Ans.push_back(i);
}
}
return(Ans);
}
Compilation message
simurgh.cpp: In function 'void solveTree(int)':
simurgh.cpp:51:53: warning: variable 't2' set but not used [-Wunused-but-set-variable]
int cur = u, seen_normal = false, t1=false, t2=false, t3=false;
^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
7552 KB |
correct |
2 |
Correct |
8 ms |
7552 KB |
correct |
3 |
Correct |
8 ms |
7552 KB |
correct |
4 |
Correct |
9 ms |
7552 KB |
correct |
5 |
Correct |
9 ms |
7552 KB |
correct |
6 |
Correct |
8 ms |
7552 KB |
correct |
7 |
Correct |
9 ms |
7552 KB |
correct |
8 |
Correct |
8 ms |
7552 KB |
correct |
9 |
Correct |
10 ms |
7552 KB |
correct |
10 |
Correct |
9 ms |
7552 KB |
correct |
11 |
Correct |
8 ms |
7552 KB |
correct |
12 |
Correct |
8 ms |
7552 KB |
correct |
13 |
Correct |
8 ms |
7552 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
7552 KB |
correct |
2 |
Correct |
8 ms |
7552 KB |
correct |
3 |
Correct |
8 ms |
7552 KB |
correct |
4 |
Correct |
9 ms |
7552 KB |
correct |
5 |
Correct |
9 ms |
7552 KB |
correct |
6 |
Correct |
8 ms |
7552 KB |
correct |
7 |
Correct |
9 ms |
7552 KB |
correct |
8 |
Correct |
8 ms |
7552 KB |
correct |
9 |
Correct |
10 ms |
7552 KB |
correct |
10 |
Correct |
9 ms |
7552 KB |
correct |
11 |
Correct |
8 ms |
7552 KB |
correct |
12 |
Correct |
8 ms |
7552 KB |
correct |
13 |
Correct |
8 ms |
7552 KB |
correct |
14 |
Correct |
11 ms |
7552 KB |
correct |
15 |
Correct |
11 ms |
7808 KB |
correct |
16 |
Correct |
11 ms |
7552 KB |
correct |
17 |
Correct |
11 ms |
7552 KB |
correct |
18 |
Correct |
10 ms |
7552 KB |
correct |
19 |
Correct |
11 ms |
7552 KB |
correct |
20 |
Correct |
11 ms |
7552 KB |
correct |
21 |
Correct |
11 ms |
7552 KB |
correct |
22 |
Correct |
10 ms |
7552 KB |
correct |
23 |
Correct |
10 ms |
7552 KB |
correct |
24 |
Correct |
11 ms |
7552 KB |
correct |
25 |
Correct |
9 ms |
7552 KB |
correct |
26 |
Correct |
10 ms |
7552 KB |
correct |
27 |
Correct |
10 ms |
7552 KB |
correct |
28 |
Correct |
10 ms |
7552 KB |
correct |
29 |
Correct |
10 ms |
7552 KB |
correct |
30 |
Correct |
10 ms |
7552 KB |
correct |
31 |
Correct |
10 ms |
7552 KB |
correct |
32 |
Correct |
10 ms |
7552 KB |
correct |
33 |
Correct |
10 ms |
7552 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
7552 KB |
correct |
2 |
Correct |
8 ms |
7552 KB |
correct |
3 |
Correct |
8 ms |
7552 KB |
correct |
4 |
Correct |
9 ms |
7552 KB |
correct |
5 |
Correct |
9 ms |
7552 KB |
correct |
6 |
Correct |
8 ms |
7552 KB |
correct |
7 |
Correct |
9 ms |
7552 KB |
correct |
8 |
Correct |
8 ms |
7552 KB |
correct |
9 |
Correct |
10 ms |
7552 KB |
correct |
10 |
Correct |
9 ms |
7552 KB |
correct |
11 |
Correct |
8 ms |
7552 KB |
correct |
12 |
Correct |
8 ms |
7552 KB |
correct |
13 |
Correct |
8 ms |
7552 KB |
correct |
14 |
Correct |
11 ms |
7552 KB |
correct |
15 |
Correct |
11 ms |
7808 KB |
correct |
16 |
Correct |
11 ms |
7552 KB |
correct |
17 |
Correct |
11 ms |
7552 KB |
correct |
18 |
Correct |
10 ms |
7552 KB |
correct |
19 |
Correct |
11 ms |
7552 KB |
correct |
20 |
Correct |
11 ms |
7552 KB |
correct |
21 |
Correct |
11 ms |
7552 KB |
correct |
22 |
Correct |
10 ms |
7552 KB |
correct |
23 |
Correct |
10 ms |
7552 KB |
correct |
24 |
Correct |
11 ms |
7552 KB |
correct |
25 |
Correct |
9 ms |
7552 KB |
correct |
26 |
Correct |
10 ms |
7552 KB |
correct |
27 |
Correct |
10 ms |
7552 KB |
correct |
28 |
Correct |
10 ms |
7552 KB |
correct |
29 |
Correct |
10 ms |
7552 KB |
correct |
30 |
Correct |
10 ms |
7552 KB |
correct |
31 |
Correct |
10 ms |
7552 KB |
correct |
32 |
Correct |
10 ms |
7552 KB |
correct |
33 |
Correct |
10 ms |
7552 KB |
correct |
34 |
Correct |
65 ms |
9208 KB |
correct |
35 |
Correct |
59 ms |
9080 KB |
correct |
36 |
Correct |
53 ms |
8704 KB |
correct |
37 |
Correct |
23 ms |
7680 KB |
correct |
38 |
Correct |
59 ms |
9080 KB |
correct |
39 |
Correct |
58 ms |
8952 KB |
correct |
40 |
Correct |
56 ms |
8696 KB |
correct |
41 |
Correct |
56 ms |
9080 KB |
correct |
42 |
Correct |
56 ms |
9080 KB |
correct |
43 |
Correct |
32 ms |
8448 KB |
correct |
44 |
Correct |
30 ms |
8192 KB |
correct |
45 |
Correct |
31 ms |
8320 KB |
correct |
46 |
Correct |
30 ms |
8192 KB |
correct |
47 |
Correct |
23 ms |
7808 KB |
correct |
48 |
Correct |
14 ms |
7552 KB |
correct |
49 |
Correct |
18 ms |
7680 KB |
correct |
50 |
Correct |
24 ms |
7808 KB |
correct |
51 |
Correct |
34 ms |
8312 KB |
correct |
52 |
Correct |
30 ms |
8192 KB |
correct |
53 |
Correct |
30 ms |
8192 KB |
correct |
54 |
Correct |
34 ms |
8576 KB |
correct |
55 |
Correct |
29 ms |
8320 KB |
correct |
56 |
Correct |
30 ms |
8320 KB |
correct |
57 |
Correct |
28 ms |
8320 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
7552 KB |
correct |
2 |
Correct |
8 ms |
7552 KB |
correct |
3 |
Correct |
153 ms |
11588 KB |
correct |
4 |
Correct |
234 ms |
14064 KB |
correct |
5 |
Correct |
256 ms |
14072 KB |
correct |
6 |
Correct |
224 ms |
14200 KB |
correct |
7 |
Correct |
219 ms |
14072 KB |
correct |
8 |
Correct |
217 ms |
14072 KB |
correct |
9 |
Correct |
237 ms |
14200 KB |
correct |
10 |
Correct |
233 ms |
14072 KB |
correct |
11 |
Correct |
227 ms |
14072 KB |
correct |
12 |
Correct |
232 ms |
14176 KB |
correct |
13 |
Correct |
8 ms |
7552 KB |
correct |
14 |
Correct |
226 ms |
14212 KB |
correct |
15 |
Correct |
239 ms |
14200 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
7552 KB |
correct |
2 |
Correct |
8 ms |
7552 KB |
correct |
3 |
Correct |
8 ms |
7552 KB |
correct |
4 |
Correct |
9 ms |
7552 KB |
correct |
5 |
Correct |
9 ms |
7552 KB |
correct |
6 |
Correct |
8 ms |
7552 KB |
correct |
7 |
Correct |
9 ms |
7552 KB |
correct |
8 |
Correct |
8 ms |
7552 KB |
correct |
9 |
Correct |
10 ms |
7552 KB |
correct |
10 |
Correct |
9 ms |
7552 KB |
correct |
11 |
Correct |
8 ms |
7552 KB |
correct |
12 |
Correct |
8 ms |
7552 KB |
correct |
13 |
Correct |
8 ms |
7552 KB |
correct |
14 |
Correct |
11 ms |
7552 KB |
correct |
15 |
Correct |
11 ms |
7808 KB |
correct |
16 |
Correct |
11 ms |
7552 KB |
correct |
17 |
Correct |
11 ms |
7552 KB |
correct |
18 |
Correct |
10 ms |
7552 KB |
correct |
19 |
Correct |
11 ms |
7552 KB |
correct |
20 |
Correct |
11 ms |
7552 KB |
correct |
21 |
Correct |
11 ms |
7552 KB |
correct |
22 |
Correct |
10 ms |
7552 KB |
correct |
23 |
Correct |
10 ms |
7552 KB |
correct |
24 |
Correct |
11 ms |
7552 KB |
correct |
25 |
Correct |
9 ms |
7552 KB |
correct |
26 |
Correct |
10 ms |
7552 KB |
correct |
27 |
Correct |
10 ms |
7552 KB |
correct |
28 |
Correct |
10 ms |
7552 KB |
correct |
29 |
Correct |
10 ms |
7552 KB |
correct |
30 |
Correct |
10 ms |
7552 KB |
correct |
31 |
Correct |
10 ms |
7552 KB |
correct |
32 |
Correct |
10 ms |
7552 KB |
correct |
33 |
Correct |
10 ms |
7552 KB |
correct |
34 |
Correct |
65 ms |
9208 KB |
correct |
35 |
Correct |
59 ms |
9080 KB |
correct |
36 |
Correct |
53 ms |
8704 KB |
correct |
37 |
Correct |
23 ms |
7680 KB |
correct |
38 |
Correct |
59 ms |
9080 KB |
correct |
39 |
Correct |
58 ms |
8952 KB |
correct |
40 |
Correct |
56 ms |
8696 KB |
correct |
41 |
Correct |
56 ms |
9080 KB |
correct |
42 |
Correct |
56 ms |
9080 KB |
correct |
43 |
Correct |
32 ms |
8448 KB |
correct |
44 |
Correct |
30 ms |
8192 KB |
correct |
45 |
Correct |
31 ms |
8320 KB |
correct |
46 |
Correct |
30 ms |
8192 KB |
correct |
47 |
Correct |
23 ms |
7808 KB |
correct |
48 |
Correct |
14 ms |
7552 KB |
correct |
49 |
Correct |
18 ms |
7680 KB |
correct |
50 |
Correct |
24 ms |
7808 KB |
correct |
51 |
Correct |
34 ms |
8312 KB |
correct |
52 |
Correct |
30 ms |
8192 KB |
correct |
53 |
Correct |
30 ms |
8192 KB |
correct |
54 |
Correct |
34 ms |
8576 KB |
correct |
55 |
Correct |
29 ms |
8320 KB |
correct |
56 |
Correct |
30 ms |
8320 KB |
correct |
57 |
Correct |
28 ms |
8320 KB |
correct |
58 |
Correct |
8 ms |
7552 KB |
correct |
59 |
Correct |
8 ms |
7552 KB |
correct |
60 |
Correct |
153 ms |
11588 KB |
correct |
61 |
Correct |
234 ms |
14064 KB |
correct |
62 |
Correct |
256 ms |
14072 KB |
correct |
63 |
Correct |
224 ms |
14200 KB |
correct |
64 |
Correct |
219 ms |
14072 KB |
correct |
65 |
Correct |
217 ms |
14072 KB |
correct |
66 |
Correct |
237 ms |
14200 KB |
correct |
67 |
Correct |
233 ms |
14072 KB |
correct |
68 |
Correct |
227 ms |
14072 KB |
correct |
69 |
Correct |
232 ms |
14176 KB |
correct |
70 |
Correct |
8 ms |
7552 KB |
correct |
71 |
Correct |
226 ms |
14212 KB |
correct |
72 |
Correct |
239 ms |
14200 KB |
correct |
73 |
Correct |
8 ms |
7552 KB |
correct |
74 |
Correct |
231 ms |
14108 KB |
correct |
75 |
Correct |
234 ms |
13992 KB |
correct |
76 |
Correct |
133 ms |
9980 KB |
correct |
77 |
Correct |
229 ms |
14072 KB |
correct |
78 |
Correct |
237 ms |
14192 KB |
correct |
79 |
Correct |
230 ms |
14072 KB |
correct |
80 |
Correct |
226 ms |
13944 KB |
correct |
81 |
Correct |
214 ms |
13432 KB |
correct |
82 |
Correct |
239 ms |
14016 KB |
correct |
83 |
Correct |
187 ms |
11000 KB |
correct |
84 |
Correct |
112 ms |
11896 KB |
correct |
85 |
Correct |
108 ms |
11512 KB |
correct |
86 |
Correct |
93 ms |
10104 KB |
correct |
87 |
Correct |
95 ms |
9592 KB |
correct |
88 |
Correct |
83 ms |
9208 KB |
correct |
89 |
Correct |
80 ms |
8952 KB |
correct |
90 |
Correct |
81 ms |
8824 KB |
correct |
91 |
Correct |
45 ms |
7800 KB |
correct |
92 |
Correct |
32 ms |
7680 KB |
correct |
93 |
Correct |
119 ms |
11616 KB |
correct |
94 |
Correct |
90 ms |
10104 KB |
correct |
95 |
Correct |
94 ms |
10104 KB |
correct |
96 |
Correct |
78 ms |
8824 KB |
correct |
97 |
Correct |
78 ms |
8952 KB |
correct |
98 |
Correct |
111 ms |
9720 KB |
correct |
99 |
Correct |
78 ms |
8952 KB |
correct |
100 |
Correct |
57 ms |
7936 KB |
correct |
101 |
Correct |
30 ms |
7680 KB |
correct |
102 |
Correct |
95 ms |
10744 KB |
correct |
103 |
Correct |
110 ms |
10744 KB |
correct |
104 |
Correct |
95 ms |
10744 KB |
correct |
105 |
Correct |
94 ms |
10744 KB |
correct |