#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<long long, int> pli;
typedef pair<int, long long> pil;
typedef vector<pair<int, int>> vii;
typedef vector<pair<long long, long long>> vll;
typedef vector<pair<long long, int>> vli;
typedef vector<pair<int, long long>> vil;
#define bitcnt(n) __builtin_popcountll((n))
#define BIT(n, i) (((n) >> (i)) & 1ll)
#define FLIP(n, i) ((n) ^ (1ll << (i)))
#define MASK(i) (1ll << (i))
#define ON(n, i) ((n) | MASK(i))
#define OFF(n, i) ((n) & ~MASK(i))
#define Int __int128
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) a / gcd(a, b) * b
#define fi first
#define se second
template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <class T1, class T2>
bool maximize(T1 &x, T2 y) {
if (x < y) {
x = y;
return true;
}
return false;
}
template <class T1, class T2>
bool minimize(T1 &x, T2 y) {
if (x > y) {
x = y;
return true;
}
return false;
}
const int N = 1e6 + 5;
const int MOD = 1e9 + 7;
const int inf = 1e9;
const ll INF = 1e18;
int n, m, dep[N], ans[N], par[N], anc[N], match[N];
bitset<N> keep;
pii edges[N];
vector<int> adj[N];
int find_set(int p) {
return par[p] == p ? p : par[p] = find_set(par[p]);
}
bool join(int u, int v) {
u = find_set(u), v = find_set(v);
if (u == v) return false;
if (dep[u] < dep[v]) swap(u, v);
par[u] = v; return true;
}
void dfs(int u) {
for (auto v : adj[u]) {
if (dep[v] == -1) {
dep[v] = dep[u] + 1;
dfs(v);
}
}
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> n >> m;
for (int i = 1; i <= n; ++i) par[i] = i;
for (int i = 1; i <= m; ++i) {
int u, v; cin >> u >> v;
edges[i] = make_pair(u, v);
}
for (int i = 1; i < n; ++i) {
int r; cin >> r;
auto [u, v] = edges[r];
adj[u].emplace_back(v), adj[v].emplace_back(u);
keep[r] = true;
}
memset(dep, -1, sizeof dep); dep[1] = 0; dfs(1);
for (int i = 1; i <= m; ++i) {
if (keep[i]) {
auto [u, v] = edges[i];
if (dep[u] < dep[v]) swap(u, v);
match[u] = i, anc[u] = v;
}
}
int w = 0;
for (int i = 1; i <= m; ++i) {
if (ans[i] != 0) continue;
auto [u, v] = edges[i];
u = find_set(u), v = find_set(v);
if (!keep[i]) {
vector<int> ve;
while (u != v) {
if (dep[u] < dep[v]) swap(u, v);
ve.emplace_back(match[u]);
join(u, anc[u]); u = find_set(u);
}
sort (ve.begin(), ve.end());
for (int i = 0; i < ve.size(); ++i) ans[ve[i]] = ++w;
}
else join(u, v);
ans[i] = ++w;
}
for (int i = 1; i <= m; ++i) cout << ans[i] << ' ';
return 0;
}
Compilation message
riggedroads.cpp: In function 'int main()':
riggedroads.cpp:124:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
124 | for (int i = 0; i < ve.size(); ++i) ans[ve[i]] = ++w;
| ~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
36956 KB |
Output is correct |
2 |
Correct |
8 ms |
36956 KB |
Output is correct |
3 |
Correct |
7 ms |
36956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
36956 KB |
Output is correct |
2 |
Correct |
8 ms |
36956 KB |
Output is correct |
3 |
Correct |
7 ms |
36956 KB |
Output is correct |
4 |
Correct |
8 ms |
36952 KB |
Output is correct |
5 |
Correct |
8 ms |
36956 KB |
Output is correct |
6 |
Correct |
8 ms |
36956 KB |
Output is correct |
7 |
Correct |
7 ms |
36988 KB |
Output is correct |
8 |
Correct |
8 ms |
36956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
43300 KB |
Output is correct |
2 |
Correct |
60 ms |
46788 KB |
Output is correct |
3 |
Correct |
58 ms |
43612 KB |
Output is correct |
4 |
Correct |
86 ms |
52416 KB |
Output is correct |
5 |
Correct |
90 ms |
53096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
48392 KB |
Output is correct |
2 |
Correct |
45 ms |
43860 KB |
Output is correct |
3 |
Correct |
24 ms |
41716 KB |
Output is correct |
4 |
Correct |
44 ms |
46896 KB |
Output is correct |
5 |
Correct |
21 ms |
42588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
141 ms |
55792 KB |
Output is correct |
2 |
Correct |
174 ms |
59640 KB |
Output is correct |
3 |
Correct |
40 ms |
44528 KB |
Output is correct |
4 |
Correct |
51 ms |
45640 KB |
Output is correct |
5 |
Correct |
164 ms |
59304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
103 ms |
51040 KB |
Output is correct |
2 |
Correct |
59 ms |
47440 KB |
Output is correct |
3 |
Correct |
163 ms |
56208 KB |
Output is correct |
4 |
Correct |
154 ms |
54684 KB |
Output is correct |
5 |
Correct |
18 ms |
40280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
36956 KB |
Output is correct |
2 |
Correct |
8 ms |
36956 KB |
Output is correct |
3 |
Correct |
7 ms |
36956 KB |
Output is correct |
4 |
Correct |
8 ms |
36952 KB |
Output is correct |
5 |
Correct |
8 ms |
36956 KB |
Output is correct |
6 |
Correct |
8 ms |
36956 KB |
Output is correct |
7 |
Correct |
7 ms |
36988 KB |
Output is correct |
8 |
Correct |
8 ms |
36956 KB |
Output is correct |
9 |
Correct |
36 ms |
43300 KB |
Output is correct |
10 |
Correct |
60 ms |
46788 KB |
Output is correct |
11 |
Correct |
58 ms |
43612 KB |
Output is correct |
12 |
Correct |
86 ms |
52416 KB |
Output is correct |
13 |
Correct |
90 ms |
53096 KB |
Output is correct |
14 |
Correct |
58 ms |
48392 KB |
Output is correct |
15 |
Correct |
45 ms |
43860 KB |
Output is correct |
16 |
Correct |
24 ms |
41716 KB |
Output is correct |
17 |
Correct |
44 ms |
46896 KB |
Output is correct |
18 |
Correct |
21 ms |
42588 KB |
Output is correct |
19 |
Correct |
141 ms |
55792 KB |
Output is correct |
20 |
Correct |
174 ms |
59640 KB |
Output is correct |
21 |
Correct |
40 ms |
44528 KB |
Output is correct |
22 |
Correct |
51 ms |
45640 KB |
Output is correct |
23 |
Correct |
164 ms |
59304 KB |
Output is correct |
24 |
Correct |
103 ms |
51040 KB |
Output is correct |
25 |
Correct |
59 ms |
47440 KB |
Output is correct |
26 |
Correct |
163 ms |
56208 KB |
Output is correct |
27 |
Correct |
154 ms |
54684 KB |
Output is correct |
28 |
Correct |
18 ms |
40280 KB |
Output is correct |
29 |
Correct |
161 ms |
58476 KB |
Output is correct |
30 |
Correct |
167 ms |
57760 KB |
Output is correct |
31 |
Correct |
174 ms |
55936 KB |
Output is correct |
32 |
Correct |
59 ms |
45512 KB |
Output is correct |
33 |
Correct |
158 ms |
56616 KB |
Output is correct |