#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using ll = long long;
const int inf = 1e9 + 100;
const ll linf = 1e18 + 100;
using vi = vector<int>;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
// #define cerr if(0)cerr
mt19937 rnd(time(0));
#define TIME (clock() * 1. / CLOCKS_PER_SEC)
using ld = long double;
const ld PI = acosl(-1.L);
const int maxn = 1e6+10;
const ll mod = 998244353;
vector<pii> g[maxn];
struct DSU {
vector<int> p;
DSU(int n): p(n) {
iota(all(p),0);
}
int f(int x){return x==p[x]?x:(p[x]=f(p[x]));}
void u(int a,int b){
a=f(a),b=f(b);
if(a==b)return;
p[b]=a;
}
};
int dep[maxn];
int par[maxn];
int parei[maxn];
void dfs(int x, int p){
par[x] = p;
for(auto [y, ei] : g[x]) if(y!=p) {
dep[y] = dep[x] + 1;
parei[y]=ei;
dfs(y,x);
}
}
void solve() {
int n,m;
cin >> n >> m;
vector<pii> eds;
for(int i=0;i<m;i++){
int a,b;
cin >> a >> b;
a--,b--;
eds.push_back({a,b});
}
set<int> spt;
for(int i=0;i<n-1;i++){
int x;
cin >> x;
spt.insert(x-1);
auto [a,b] = eds[x-1];
g[a].push_back({b,x-1});
g[b].push_back({a,x-1});
}
dfs(0,-1);
DSU dsu(n);
int cur = 0;
vector<int> ans(m,-1);
for(int i=0;i<m;i++){
if(ans[i]!=-1)continue;
auto [a,b] = eds[i];
if(spt.count(i)) {
ans[i]=cur++;
auto [a,b] = eds[i];
if(dep[a]>dep[b])swap(a,b);
dsu.u(a,b);
// cerr << i << " " << ans[i] << endl;
continue;
}
a=dsu.f(a),b=dsu.f(b);
vector<int> ve;
while(a!=b) {
if(dep[a]>dep[b]) {
dsu.u(par[a],a);
ve.push_back(parei[a]);
a=dsu.f(a);
}
else {
dsu.u(par[b],b);
ve.push_back(parei[b]);
b=dsu.f(b);
}
}
sort(all(ve));
for(int i=0;i<ve.size();i++){
// cerr << ve[i] << " " << cur+i << endl;
ans[ve[i]]=cur+i;
}
cur+=ve.size();
ans[i]=cur++;
}
for(int i=0;i<m;i++)cout << ans[i]+1 << " \n"[i==m-1];
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
cout << fixed;
cout.precision(9);
solve();
}
Compilation message
riggedroads.cpp: In function 'void solve()':
riggedroads.cpp:102:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for(int i=0;i<ve.size();i++){
| ~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23896 KB |
Output is correct |
2 |
Correct |
12 ms |
23896 KB |
Output is correct |
3 |
Correct |
11 ms |
23900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23896 KB |
Output is correct |
2 |
Correct |
12 ms |
23896 KB |
Output is correct |
3 |
Correct |
11 ms |
23900 KB |
Output is correct |
4 |
Correct |
12 ms |
24036 KB |
Output is correct |
5 |
Correct |
12 ms |
24012 KB |
Output is correct |
6 |
Correct |
13 ms |
23900 KB |
Output is correct |
7 |
Correct |
12 ms |
23900 KB |
Output is correct |
8 |
Correct |
11 ms |
23896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
37088 KB |
Output is correct |
2 |
Correct |
121 ms |
42772 KB |
Output is correct |
3 |
Correct |
73 ms |
33988 KB |
Output is correct |
4 |
Correct |
218 ms |
60844 KB |
Output is correct |
5 |
Correct |
228 ms |
62768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
99 ms |
45768 KB |
Output is correct |
2 |
Correct |
52 ms |
33232 KB |
Output is correct |
3 |
Correct |
31 ms |
28628 KB |
Output is correct |
4 |
Correct |
81 ms |
39884 KB |
Output is correct |
5 |
Correct |
33 ms |
30172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
242 ms |
64596 KB |
Output is correct |
2 |
Correct |
275 ms |
70948 KB |
Output is correct |
3 |
Correct |
59 ms |
37288 KB |
Output is correct |
4 |
Correct |
93 ms |
43724 KB |
Output is correct |
5 |
Correct |
332 ms |
74984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
185 ms |
52936 KB |
Output is correct |
2 |
Correct |
108 ms |
43724 KB |
Output is correct |
3 |
Correct |
339 ms |
67816 KB |
Output is correct |
4 |
Correct |
283 ms |
64656 KB |
Output is correct |
5 |
Correct |
26 ms |
27612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23896 KB |
Output is correct |
2 |
Correct |
12 ms |
23896 KB |
Output is correct |
3 |
Correct |
11 ms |
23900 KB |
Output is correct |
4 |
Correct |
12 ms |
24036 KB |
Output is correct |
5 |
Correct |
12 ms |
24012 KB |
Output is correct |
6 |
Correct |
13 ms |
23900 KB |
Output is correct |
7 |
Correct |
12 ms |
23900 KB |
Output is correct |
8 |
Correct |
11 ms |
23896 KB |
Output is correct |
9 |
Correct |
76 ms |
37088 KB |
Output is correct |
10 |
Correct |
121 ms |
42772 KB |
Output is correct |
11 |
Correct |
73 ms |
33988 KB |
Output is correct |
12 |
Correct |
218 ms |
60844 KB |
Output is correct |
13 |
Correct |
228 ms |
62768 KB |
Output is correct |
14 |
Correct |
99 ms |
45768 KB |
Output is correct |
15 |
Correct |
52 ms |
33232 KB |
Output is correct |
16 |
Correct |
31 ms |
28628 KB |
Output is correct |
17 |
Correct |
81 ms |
39884 KB |
Output is correct |
18 |
Correct |
33 ms |
30172 KB |
Output is correct |
19 |
Correct |
242 ms |
64596 KB |
Output is correct |
20 |
Correct |
275 ms |
70948 KB |
Output is correct |
21 |
Correct |
59 ms |
37288 KB |
Output is correct |
22 |
Correct |
93 ms |
43724 KB |
Output is correct |
23 |
Correct |
332 ms |
74984 KB |
Output is correct |
24 |
Correct |
185 ms |
52936 KB |
Output is correct |
25 |
Correct |
108 ms |
43724 KB |
Output is correct |
26 |
Correct |
339 ms |
67816 KB |
Output is correct |
27 |
Correct |
283 ms |
64656 KB |
Output is correct |
28 |
Correct |
26 ms |
27612 KB |
Output is correct |
29 |
Correct |
295 ms |
63932 KB |
Output is correct |
30 |
Correct |
336 ms |
65472 KB |
Output is correct |
31 |
Correct |
345 ms |
63376 KB |
Output is correct |
32 |
Correct |
66 ms |
33996 KB |
Output is correct |
33 |
Correct |
329 ms |
63828 KB |
Output is correct |