#include <bits/stdc++.h>
#define fi first
#define se second
#define all(v) v.begin() , v.end()
#define sz(v) int(v.size())
#define unq(v) sort(all(v)); v.resize(unique(all(v)) - v.begin());
using namespace std;
typedef long long ll;
typedef pair<int , int> ii;
typedef pair<long long , int> lli;
const int maxN = int(3e5)+7;
int n , m;
vector<int> g[maxN];
ii E[maxN];
bool used[maxN];
int fa[maxN] , nxt[maxN] , p[maxN] , h[maxN] , res[maxN] , cur = 0;
void dfs(int u , int par){
for (int id : g[u]){
int v = u ^ (E[id].fi ^ E[id].se);
if (v != par){
p[v] = id;
h[v] = h[u] + 1;
dfs(v , u);
}
}
}
int root(int x){
if (fa[x] < 0) return x; else return fa[x] = root(fa[x]);
}
void unite(int u , int v){
u = root(u);
v = root(v);
if (u == v) return;
if (-fa[u] < -fa[v]) swap(u , v);
fa[u] += fa[v];
fa[v] = u;
if (h[nxt[u]] > h[nxt[v]]) nxt[u] = nxt[v];
}
vector<int> pos;
void get(int u , int v){
pos.clear();
u = root(u);
v = root(v);
while (u != v){
if (h[u] > h[v]) swap(u , v);
int id = p[v];
if (res[id] == 0) pos.push_back(id);
int nxt_v = v ^ (E[id].fi ^ E[id].se);
unite(v , nxt_v);
v = nxt[root(v)];
}
}
void solve(){
cin >> n >> m;
for (int i = 1 ; i <= m ; i++){
int u , v;
cin >> u >> v;
E[i] = {u , v};
}
for (int i = 1 ; i < n ; i++){
int x; cin >> x;
int u = E[x].fi;
int v = E[x].se;
used[x] = 1;
g[u].push_back(x);
g[v].push_back(x);
//cout << E[x].fi << " " << E[x].se << '\n';
}
for (int i = 1 ; i <= n ; i++){
fa[i] = -1;
nxt[i] = i;
}
dfs(1 , 0);
for (int i = 1 ; i <= m ; i++){
if (used[i] == 0){
get(E[i].fi , E[i].se);
sort(all(pos));
for (int x : pos) res[x] = ++cur;
res[i] = ++cur;
}
else{
if (res[i] == 0) res[i] = ++cur;
}
}
for (int i = 1 ; i <= m ; i++) cout << res[i] << " ";
}
#define name "A"
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(name".INP" , "r")){
freopen(name".INP" , "r" , stdin);
freopen(name".OUT" , "w" , stdout);
}
int t = 1; //cin >> t;
while (t--) solve();
return 0;
}
Compilation message
riggedroads.cpp: In function 'int main()':
riggedroads.cpp:102:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
102 | freopen(name".INP" , "r" , stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
riggedroads.cpp:103:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
103 | freopen(name".OUT" , "w" , stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
11600 KB |
Output is correct |
2 |
Correct |
3 ms |
11600 KB |
Output is correct |
3 |
Correct |
3 ms |
11768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
11600 KB |
Output is correct |
2 |
Correct |
3 ms |
11600 KB |
Output is correct |
3 |
Correct |
3 ms |
11768 KB |
Output is correct |
4 |
Correct |
3 ms |
11600 KB |
Output is correct |
5 |
Correct |
3 ms |
11600 KB |
Output is correct |
6 |
Correct |
5 ms |
11600 KB |
Output is correct |
7 |
Correct |
3 ms |
11600 KB |
Output is correct |
8 |
Correct |
3 ms |
11600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
18632 KB |
Output is correct |
2 |
Correct |
72 ms |
20560 KB |
Output is correct |
3 |
Correct |
50 ms |
17232 KB |
Output is correct |
4 |
Correct |
81 ms |
28088 KB |
Output is correct |
5 |
Correct |
83 ms |
28748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
27312 KB |
Output is correct |
2 |
Correct |
36 ms |
20560 KB |
Output is correct |
3 |
Correct |
20 ms |
17224 KB |
Output is correct |
4 |
Correct |
46 ms |
25408 KB |
Output is correct |
5 |
Correct |
20 ms |
18500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
134 ms |
36428 KB |
Output is correct |
2 |
Correct |
126 ms |
47552 KB |
Output is correct |
3 |
Correct |
35 ms |
22980 KB |
Output is correct |
4 |
Correct |
54 ms |
27460 KB |
Output is correct |
5 |
Correct |
182 ms |
46276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
91 ms |
31168 KB |
Output is correct |
2 |
Correct |
57 ms |
27720 KB |
Output is correct |
3 |
Correct |
154 ms |
41328 KB |
Output is correct |
4 |
Correct |
147 ms |
38472 KB |
Output is correct |
5 |
Correct |
14 ms |
15440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
11600 KB |
Output is correct |
2 |
Correct |
3 ms |
11600 KB |
Output is correct |
3 |
Correct |
3 ms |
11768 KB |
Output is correct |
4 |
Correct |
3 ms |
11600 KB |
Output is correct |
5 |
Correct |
3 ms |
11600 KB |
Output is correct |
6 |
Correct |
5 ms |
11600 KB |
Output is correct |
7 |
Correct |
3 ms |
11600 KB |
Output is correct |
8 |
Correct |
3 ms |
11600 KB |
Output is correct |
9 |
Correct |
31 ms |
18632 KB |
Output is correct |
10 |
Correct |
72 ms |
20560 KB |
Output is correct |
11 |
Correct |
50 ms |
17232 KB |
Output is correct |
12 |
Correct |
81 ms |
28088 KB |
Output is correct |
13 |
Correct |
83 ms |
28748 KB |
Output is correct |
14 |
Correct |
77 ms |
27312 KB |
Output is correct |
15 |
Correct |
36 ms |
20560 KB |
Output is correct |
16 |
Correct |
20 ms |
17224 KB |
Output is correct |
17 |
Correct |
46 ms |
25408 KB |
Output is correct |
18 |
Correct |
20 ms |
18500 KB |
Output is correct |
19 |
Correct |
134 ms |
36428 KB |
Output is correct |
20 |
Correct |
126 ms |
47552 KB |
Output is correct |
21 |
Correct |
35 ms |
22980 KB |
Output is correct |
22 |
Correct |
54 ms |
27460 KB |
Output is correct |
23 |
Correct |
182 ms |
46276 KB |
Output is correct |
24 |
Correct |
91 ms |
31168 KB |
Output is correct |
25 |
Correct |
57 ms |
27720 KB |
Output is correct |
26 |
Correct |
154 ms |
41328 KB |
Output is correct |
27 |
Correct |
147 ms |
38472 KB |
Output is correct |
28 |
Correct |
14 ms |
15440 KB |
Output is correct |
29 |
Correct |
170 ms |
39112 KB |
Output is correct |
30 |
Correct |
279 ms |
37844 KB |
Output is correct |
31 |
Correct |
163 ms |
34140 KB |
Output is correct |
32 |
Correct |
54 ms |
20808 KB |
Output is correct |
33 |
Correct |
140 ms |
35144 KB |
Output is correct |