#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cassert>
#include <tuple>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <numeric>
using namespace std;
typedef long long LL;
typedef vector<int> Vi;
typedef vector<LL> VL;
typedef vector<bool> Vb;
typedef vector<vector<int>> Vii;
typedef vector<vector<vector<int>>> Viii;
typedef vector<vector<pair<int, int>>> Vip;
#define forR(i, n) for (int i = 0; i < (n); i++)
int count_common_roads(const Vi& r);
Vip nbs; // {nb, edge}
Viii nbgps; // index
int N;
Vi pathProp; // 0: unknown; 1: royale; 2: not royale
Vb pathPending;
Vi parent; // disjoint set
int getRoot(int x) {
if (x == parent[x]) return x;
int ppx = getRoot(parent[x]);
parent[x] = ppx;
return ppx;
}
void setEq(int x, int y) {
int px = getRoot(x), py = getRoot(y);
parent[py] = px;
if (pathProp[py] > 0) {
assert(pathProp[px] == 0 || pathProp[px] == pathProp[py]);
pathProp[px] = pathProp[py];
}
pathPending[px] = pathPending[py] = true;
}
void set_prop(int e, int prop) {
int re = getRoot(e);
assert(pathProp[re] == 0 || pathProp[re] == prop);
pathProp[re] = prop;
}
int get_prop(int e) {
int re = getRoot(e);
assert(pathProp[re] > 0);
return pathProp[re];
}
bool has_prop(int e) {
int re = getRoot(e);
return pathProp[re] > 0;
}
pair<Vi, Vb> gen_tree(int root, int node = -1) {
Vb vis(N); vis[root] = true;
queue<int> q;
if (node < 0) {
for (auto& gps : nbgps[root]) {
int node = nbs[root][gps[0]].first;
vis[node] = true;
q.push(node);
}
} else {
vis[node] = true;
q.push(node);
}
Vi edges;
while (!q.empty()) {
int x = q.front(); q.pop();
for (auto nb : nbs[x]) {
if (!vis[nb.first]) {
vis[nb.first] = true;
q.push(nb.first);
edges.push_back(nb.second);
}
}
}
if (node < 0) {
assert(edges.size() == N - 1 - nbgps[root].size());
}
return { edges, vis };
}
Vi find_roads(int n, Vi u, Vi v) {
nbs = Vip(n);
nbgps = Viii(n);
N = n;
int m = u.size();
pathProp = Vi(m);
pathPending = Vb(m);
parent = Vi();
forR(i, m) parent.push_back(i);
forR(i, m) {
nbs[u[i]].push_back({ v[i], i });
nbs[v[i]].push_back({ u[i], i });
}
forR(i, n) {
Vb visited(n);
forR(j, nbs[i].size()) {
int node = nbs[i][j].first;
if (visited[node]) continue;
auto pair = gen_tree(i, node);
Vb& vis = pair.second;
nbgps[i].push_back(Vi());
forR(k, nbs[i].size()) {
int nk = nbs[i][k].first;
if (vis[nk]) {
nbgps[i].back().push_back(k);
visited[nk] = true;
}
}
}
}
forR(i, n) {
Vi sTree = gen_tree(i).first;
Vi sTree0 = sTree; // all first edges
forR(k, nbgps[i].size()) {
int edge = nbs[i][nbgps[i][k][0]].second;
sTree0.push_back(edge);
}
int val0 = count_common_roads(sTree0);
forR(igp, nbgps[i].size()) {
auto& gp = nbgps[i][igp];
if (gp.size() == 1) {
set_prop(nbs[i][gp[0]].second, 1);
continue;
}
Vi gpTree = sTree;
forR(k, nbgps[i].size()) { // edges of other groups
if (k == igp) continue;
int edge = nbs[i][nbgps[i][k][0]].second;
gpTree.push_back(edge);
}
int edge0 = nbs[i][gp[0]].second;
bool someHasProp = has_prop(edge0);
for (int j = 1; j < gp.size(); j++) {
int edge = nbs[i][gp[j]].second;
if (has_prop(edge)) {
if (!someHasProp) someHasProp = true;
else continue;
}
gpTree.push_back(edge);
int valJ = count_common_roads(gpTree);
gpTree.pop_back();
if (val0 == valJ) {
setEq(edge0, edge);
} else if (val0 < valJ) { // j royale
set_prop(edge0, 2);
set_prop(edge, 1);
} else { // 0 royale
set_prop(edge0, 1);
set_prop(edge, 2);
}
}
if (!has_prop(edge0)) {
// pending edge0: all pending => all set to 1
set_prop(edge0, 1);
}
}
}
Vi royaleEdges;
forR(i, m) if (get_prop(i) == 1) royaleEdges.push_back(i);
assert(royaleEdges.size() == n - 1);
return royaleEdges;
}
#ifdef TEST_LOCAL
set<int> _ry{ 5,1,0 };
int count_common_roads(const Vi& r) {
int cnt = 0;
for (int e : r)
if (_ry.count(e) > 0)
cnt++;
return cnt;
}
int main() {
auto r = find_roads(4, Vi{ 0, 0, 0, 1, 1, 2 }, Vi{ 1, 2, 3, 3, 2, 3 }); //
return 0;
}
#endif
Compilation message
simurgh.cpp: In function 'Vi find_roads(int, Vi, Vi)':
simurgh.cpp:28:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | #define forR(i, n) for (int i = 0; i < (n); i++)
| ^
simurgh.cpp:120:3: note: in expansion of macro 'forR'
120 | forR(j, nbs[i].size()) {
| ^~~~
simurgh.cpp:28:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | #define forR(i, n) for (int i = 0; i < (n); i++)
| ^
simurgh.cpp:127:4: note: in expansion of macro 'forR'
127 | forR(k, nbs[i].size()) {
| ^~~~
simurgh.cpp:28:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | #define forR(i, n) for (int i = 0; i < (n); i++)
| ^
simurgh.cpp:141:3: note: in expansion of macro 'forR'
141 | forR(k, nbgps[i].size()) {
| ^~~~
simurgh.cpp:28:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | #define forR(i, n) for (int i = 0; i < (n); i++)
| ^
simurgh.cpp:147:3: note: in expansion of macro 'forR'
147 | forR(igp, nbgps[i].size()) {
| ^~~~
simurgh.cpp:28:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | #define forR(i, n) for (int i = 0; i < (n); i++)
| ^
simurgh.cpp:155:4: note: in expansion of macro 'forR'
155 | forR(k, nbgps[i].size()) { // edges of other groups
| ^~~~
simurgh.cpp:164:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
164 | for (int j = 1; j < gp.size(); j++) {
| ~~^~~~~~~~~~~
In file included from /usr/include/c++/9/cassert:44,
from simurgh.cpp:6:
simurgh.cpp:194:28: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
194 | assert(royaleEdges.size() == n - 1);
| ~~~~~~~~~~~~~~~~~~~^~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
correct |
2 |
Correct |
1 ms |
204 KB |
correct |
3 |
Correct |
1 ms |
272 KB |
correct |
4 |
Correct |
1 ms |
204 KB |
correct |
5 |
Correct |
1 ms |
204 KB |
correct |
6 |
Correct |
1 ms |
204 KB |
correct |
7 |
Correct |
1 ms |
204 KB |
correct |
8 |
Correct |
1 ms |
204 KB |
correct |
9 |
Correct |
1 ms |
204 KB |
correct |
10 |
Correct |
1 ms |
204 KB |
correct |
11 |
Correct |
1 ms |
204 KB |
correct |
12 |
Correct |
1 ms |
204 KB |
correct |
13 |
Correct |
1 ms |
204 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
correct |
2 |
Correct |
1 ms |
204 KB |
correct |
3 |
Correct |
1 ms |
272 KB |
correct |
4 |
Correct |
1 ms |
204 KB |
correct |
5 |
Correct |
1 ms |
204 KB |
correct |
6 |
Correct |
1 ms |
204 KB |
correct |
7 |
Correct |
1 ms |
204 KB |
correct |
8 |
Correct |
1 ms |
204 KB |
correct |
9 |
Correct |
1 ms |
204 KB |
correct |
10 |
Correct |
1 ms |
204 KB |
correct |
11 |
Correct |
1 ms |
204 KB |
correct |
12 |
Correct |
1 ms |
204 KB |
correct |
13 |
Correct |
1 ms |
204 KB |
correct |
14 |
Correct |
3 ms |
332 KB |
correct |
15 |
Correct |
3 ms |
332 KB |
correct |
16 |
Correct |
3 ms |
300 KB |
correct |
17 |
Correct |
4 ms |
384 KB |
correct |
18 |
Correct |
2 ms |
204 KB |
correct |
19 |
Correct |
3 ms |
332 KB |
correct |
20 |
Correct |
3 ms |
332 KB |
correct |
21 |
Correct |
3 ms |
332 KB |
correct |
22 |
Correct |
3 ms |
332 KB |
correct |
23 |
Correct |
3 ms |
336 KB |
correct |
24 |
Correct |
2 ms |
332 KB |
correct |
25 |
Correct |
1 ms |
204 KB |
correct |
26 |
Correct |
2 ms |
332 KB |
correct |
27 |
Correct |
2 ms |
332 KB |
correct |
28 |
Correct |
1 ms |
204 KB |
correct |
29 |
Correct |
1 ms |
204 KB |
correct |
30 |
Correct |
2 ms |
332 KB |
correct |
31 |
Correct |
2 ms |
332 KB |
correct |
32 |
Correct |
3 ms |
332 KB |
correct |
33 |
Correct |
3 ms |
332 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
correct |
2 |
Correct |
1 ms |
204 KB |
correct |
3 |
Correct |
1 ms |
272 KB |
correct |
4 |
Correct |
1 ms |
204 KB |
correct |
5 |
Correct |
1 ms |
204 KB |
correct |
6 |
Correct |
1 ms |
204 KB |
correct |
7 |
Correct |
1 ms |
204 KB |
correct |
8 |
Correct |
1 ms |
204 KB |
correct |
9 |
Correct |
1 ms |
204 KB |
correct |
10 |
Correct |
1 ms |
204 KB |
correct |
11 |
Correct |
1 ms |
204 KB |
correct |
12 |
Correct |
1 ms |
204 KB |
correct |
13 |
Correct |
1 ms |
204 KB |
correct |
14 |
Correct |
3 ms |
332 KB |
correct |
15 |
Correct |
3 ms |
332 KB |
correct |
16 |
Correct |
3 ms |
300 KB |
correct |
17 |
Correct |
4 ms |
384 KB |
correct |
18 |
Correct |
2 ms |
204 KB |
correct |
19 |
Correct |
3 ms |
332 KB |
correct |
20 |
Correct |
3 ms |
332 KB |
correct |
21 |
Correct |
3 ms |
332 KB |
correct |
22 |
Correct |
3 ms |
332 KB |
correct |
23 |
Correct |
3 ms |
336 KB |
correct |
24 |
Correct |
2 ms |
332 KB |
correct |
25 |
Correct |
1 ms |
204 KB |
correct |
26 |
Correct |
2 ms |
332 KB |
correct |
27 |
Correct |
2 ms |
332 KB |
correct |
28 |
Correct |
1 ms |
204 KB |
correct |
29 |
Correct |
1 ms |
204 KB |
correct |
30 |
Correct |
2 ms |
332 KB |
correct |
31 |
Correct |
2 ms |
332 KB |
correct |
32 |
Correct |
3 ms |
332 KB |
correct |
33 |
Correct |
3 ms |
332 KB |
correct |
34 |
Correct |
275 ms |
1748 KB |
correct |
35 |
Correct |
277 ms |
1736 KB |
correct |
36 |
Correct |
191 ms |
1508 KB |
correct |
37 |
Correct |
21 ms |
332 KB |
correct |
38 |
Correct |
264 ms |
1740 KB |
correct |
39 |
Correct |
237 ms |
1640 KB |
correct |
40 |
Correct |
181 ms |
1484 KB |
correct |
41 |
Correct |
258 ms |
1748 KB |
correct |
42 |
Correct |
261 ms |
1736 KB |
correct |
43 |
Correct |
140 ms |
1228 KB |
correct |
44 |
Correct |
112 ms |
952 KB |
correct |
45 |
Correct |
134 ms |
1084 KB |
correct |
46 |
Correct |
98 ms |
908 KB |
correct |
47 |
Correct |
47 ms |
692 KB |
correct |
48 |
Correct |
8 ms |
332 KB |
correct |
49 |
Correct |
16 ms |
412 KB |
correct |
50 |
Correct |
47 ms |
588 KB |
correct |
51 |
Correct |
131 ms |
980 KB |
correct |
52 |
Correct |
116 ms |
952 KB |
correct |
53 |
Correct |
100 ms |
908 KB |
correct |
54 |
Correct |
142 ms |
1272 KB |
correct |
55 |
Correct |
138 ms |
1008 KB |
correct |
56 |
Correct |
139 ms |
1100 KB |
correct |
57 |
Correct |
160 ms |
964 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
correct |
2 |
Correct |
1 ms |
204 KB |
correct |
3 |
Incorrect |
295 ms |
4784 KB |
WA in grader: NO |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
correct |
2 |
Correct |
1 ms |
204 KB |
correct |
3 |
Correct |
1 ms |
272 KB |
correct |
4 |
Correct |
1 ms |
204 KB |
correct |
5 |
Correct |
1 ms |
204 KB |
correct |
6 |
Correct |
1 ms |
204 KB |
correct |
7 |
Correct |
1 ms |
204 KB |
correct |
8 |
Correct |
1 ms |
204 KB |
correct |
9 |
Correct |
1 ms |
204 KB |
correct |
10 |
Correct |
1 ms |
204 KB |
correct |
11 |
Correct |
1 ms |
204 KB |
correct |
12 |
Correct |
1 ms |
204 KB |
correct |
13 |
Correct |
1 ms |
204 KB |
correct |
14 |
Correct |
3 ms |
332 KB |
correct |
15 |
Correct |
3 ms |
332 KB |
correct |
16 |
Correct |
3 ms |
300 KB |
correct |
17 |
Correct |
4 ms |
384 KB |
correct |
18 |
Correct |
2 ms |
204 KB |
correct |
19 |
Correct |
3 ms |
332 KB |
correct |
20 |
Correct |
3 ms |
332 KB |
correct |
21 |
Correct |
3 ms |
332 KB |
correct |
22 |
Correct |
3 ms |
332 KB |
correct |
23 |
Correct |
3 ms |
336 KB |
correct |
24 |
Correct |
2 ms |
332 KB |
correct |
25 |
Correct |
1 ms |
204 KB |
correct |
26 |
Correct |
2 ms |
332 KB |
correct |
27 |
Correct |
2 ms |
332 KB |
correct |
28 |
Correct |
1 ms |
204 KB |
correct |
29 |
Correct |
1 ms |
204 KB |
correct |
30 |
Correct |
2 ms |
332 KB |
correct |
31 |
Correct |
2 ms |
332 KB |
correct |
32 |
Correct |
3 ms |
332 KB |
correct |
33 |
Correct |
3 ms |
332 KB |
correct |
34 |
Correct |
275 ms |
1748 KB |
correct |
35 |
Correct |
277 ms |
1736 KB |
correct |
36 |
Correct |
191 ms |
1508 KB |
correct |
37 |
Correct |
21 ms |
332 KB |
correct |
38 |
Correct |
264 ms |
1740 KB |
correct |
39 |
Correct |
237 ms |
1640 KB |
correct |
40 |
Correct |
181 ms |
1484 KB |
correct |
41 |
Correct |
258 ms |
1748 KB |
correct |
42 |
Correct |
261 ms |
1736 KB |
correct |
43 |
Correct |
140 ms |
1228 KB |
correct |
44 |
Correct |
112 ms |
952 KB |
correct |
45 |
Correct |
134 ms |
1084 KB |
correct |
46 |
Correct |
98 ms |
908 KB |
correct |
47 |
Correct |
47 ms |
692 KB |
correct |
48 |
Correct |
8 ms |
332 KB |
correct |
49 |
Correct |
16 ms |
412 KB |
correct |
50 |
Correct |
47 ms |
588 KB |
correct |
51 |
Correct |
131 ms |
980 KB |
correct |
52 |
Correct |
116 ms |
952 KB |
correct |
53 |
Correct |
100 ms |
908 KB |
correct |
54 |
Correct |
142 ms |
1272 KB |
correct |
55 |
Correct |
138 ms |
1008 KB |
correct |
56 |
Correct |
139 ms |
1100 KB |
correct |
57 |
Correct |
160 ms |
964 KB |
correct |
58 |
Correct |
1 ms |
204 KB |
correct |
59 |
Correct |
1 ms |
204 KB |
correct |
60 |
Incorrect |
295 ms |
4784 KB |
WA in grader: NO |
61 |
Halted |
0 ms |
0 KB |
- |