///*** Sown_Vipro ***///
/// ->NHI QUOC GIA<- ///
#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("popcnt")
#define F first
#define S second
#define pb push_back
#define pi pair<int, int>
#define pii pair<int, pair<int, int> >
#define FOR(i, a, b) for(int i = a; i <= b; ++i)
#define REP(i, a, b) for(int i = a; i >= b; --i)
#define all(s) s.begin(), s.end()
#define szz(s) int(s.size())
const string NAME = "sown";
const int N = 1e6 + 5, MAX = 1e6, oo = 1e9 + 5, MOD = 1e9 + 7;
void maxi(int &x, int y){ if(x < y) x = y; }
void mini(int &x, int y){ if(x > y) x = y; };
void add(int &x, int y){ x += y; x += MOD * (x < 0); x -= MOD * (x >= MOD); };
int n, m, cur, root;
int p[20][N], idx[N], del[N], h[N], check[N], ans[N], nxt[N];
pi e[N];
vector<pi> adj[N];
void pre(int u){
h[u] = h[p[0][u]] + 1;
for(auto [v, i] : adj[u]){
if(v != p[0][u]){
p[0][v] = u;
nxt[v] = u;
idx[v] = i;
pre(v);
}
}
}
int lca(int u, int v){
if(h[u] < h[v]) swap(u, v);
REP(k, 18, 0){
int pu = p[k][u];
if(h[pu] >= h[v]) u = pu;
}
if(u == v) return u;
REP(k, 18, 0){
int pu = p[k][u], pv = p[k][v];
if(pu != pv){
u = pu;
v = pv;
}
}
return p[0][u];
}
int Find(int u){
if(!del[u]) return u;
return nxt[u] = Find(nxt[u]);
}
void solve(){
cin >> n >> m;
FOR(i, 1, m){
int u, v; cin >> u >> v;
e[i] = {u, v};
}
FOR(i, 1, n - 1){
int t; cin >> t;
check[t] = 1;
root = e[t].F;
adj[e[t].F].pb({e[t].S, t});
adj[e[t].S].pb({e[t].F, t});
}
pre(root);
FOR(k, 1, 18){
FOR(u, 1, n) p[k][u] = p[k - 1][p[k - 1][u]];
}
// FOR(u, 1, n) cout << u << " " << nxt[u] << " " << idx[u] << "\n";
FOR(i, 1, m){
int u = e[i].F, v = e[i].S;
if(ans[i]) continue;
if(check[i]){
if(idx[u] == i) del[u] = 1;
if(idx[v] == i) del[v] = 1;
ans[i] = ++cur;
continue;
}
int w = lca(u, v);
// cout << w << "\n";
vector<int> t;
while(1){
u = Find(u);
if(h[u] <= h[w]) break;
// cout << u << " " << idx[u] << "\n";
t.pb(idx[u]);
del[u] = 1;
}
while(1){
v = Find(v);
if(h[v] <= h[w]) break;
// cout << v << " " << idx[v] << "\n";
t.pb(idx[v]);
del[v] = 1;
}
sort(all(t));
for(int id : t){
ans[id] = ++cur;
}
ans[i] = ++cur;
// break;
}
FOR(i, 1, m) cout << ans[i] << " ";
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
if(fopen((NAME + ".inp").c_str(), "r")){
freopen((NAME + ".inp").c_str(), "r", stdin);
// freopen((NAME + ".out").c_str(), "w", stdout);
}
int t = 1;
// cin >> t;
while(t--){
solve();
}
}
Compilation message (stderr)
riggedroads.cpp: In function 'int main()':
riggedroads.cpp:126:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
126 | freopen((NAME + ".inp").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |