// Programmer: Shadow1
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using str = string; // yay python!
#define i64 int64_t
#define show(x) cerr << (#x) << " = " << (x) << '\n';
#define output_vector(v) for(auto &x : v){cout << x << ' ';}cout << '\n';
#define output_pairvector(v) for(auto &x : v){cout << x.first << " " << x.second << '\n';}
#define read_vector(v) for(auto &x : v){cin >> x;}
#define vt vector
#define prq priority_queue
#define pb push_back
#define eb emplace_back
#define pii pair<int,int>
#define fir first
#define sec second
#define sz(x) ll(x.size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define int long long
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
// Riggedroads
int w = 1;
const int maxn = 3e5 + 5;
vector<pii> e(maxn), adj[maxn], par(maxn);
vector<int> dep(maxn), ans(maxn), r(maxn);
vector<bool> vis(maxn); // visited edges
int twok[maxn][25]; //preset to -1
void init(int x, int p) {
for (int k = 0; k < 25; ++k) {
if (twok[x][k] == -1) break;
twok[x][k+1] = twok[twok[x][k]][k];
}
for (auto &it:adj[x]) {
if(it.fir == p) continue;
twok[it.fir][0] = x;
dep[it.fir] = dep[x] + 1;
init(it.fir,x);
}
}
int kpar(int x, int k){
for(int j=0; j<25; j++){
if(x==-1) break;
if(k&(1<<j)) x=twok[x][j];
}
return x;
}
int lca(int a,int b){
if (dep[a] < dep[b]) swap(a,b);
a = kpar(a, dep[a] - dep[b]);
if (a == b) return a; // edge case where b is an ancestor of a
for (int k=24;k>=0;k--){
if (twok[a][k] != twok[b][k]){
a = twok[a][k]; b = twok[b][k];
}
}
return twok[a][0];
}
void dfs(int u, int p) {
for(auto &v : adj[u]) {
if(v.fir == p) continue;
dep[v.fir] = dep[u] + 1;
par[v.fir].fir = u;
par[v.fir].sec = v.sec;
dfs(v.fir, u);
}
}
void ban(int u, int v, int k) { // deactivate edge (u, v)
if(u == v || u == 0) return;
// assume u is parent of v i.e. par[v] = u
if(dep[u] > dep[v]) swap(u, v);
par[v] = par[u];
vis[k] = true;
}
void move(int u, int v) {
// if(same(u, v)) return;
vector<int> span;
int x = lca(u, v);
while(dep[par[u].fir] >= dep[x]) {
span.push_back(par[u].sec);
int op = par[u].fir;
ban(par[u].fir, u, par[u].sec);
u = op;
}
while(dep[par[v].fir] >= dep[x]) {
span.push_back(par[v].sec);
int op = par[v].fir;
ban(par[v].fir, v, par[v].sec);
v =op;
}
sort(all(span));
for(auto &k : span) {
// show(k);
if(k != -1) {
ans[k] = w++;
}
}
}
void solve() {
int n, m;
cin >> n >> m;
for(int i=0; i<m; ++i) {
int a, b;
cin >> a >> b;
e[i] = {a, b};
}
vector<bool> keep(m);
for(int i=0; i<n-1; ++i) {
cin >> r[i];
r[i]--;
keep[r[i]] = true;
}
for(int i=0; i<m; ++i) {
if(keep[i]) {
int a = e[i].fir, b = e[i].sec;
adj[a].push_back({b, i});
adj[b].push_back({a, i});
}
}
memset(twok, -1, sizeof(twok));
init(1, 0);
par[1] = {0, -1};
dep[1] = 1;
dfs(1, 0);
for(int i=0; i<m; ++i) {
if(vis[i]) continue;
int u = e[i].fir, v = e[i].sec;
if(!keep[i]) {
move(u, v);
ans[i] = w++;
} else {
ban(u, v, i);
ans[i] = w++;
}
}
for(int i=0; i<m; ++i)
cout << ans[i] << " ";
}
// CHECK YOUR OVERFLOWS!!!!
signed main() {
// freopen("output.txt", "w", stdout);
// freopen("input.txt", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(NULL);
int T = 1;
// cin >> T;
for(int tc = 1; tc <= T; ++tc) {
// cout << "Case #" << tc << ": ";
solve();
}
return 0;
}
# | 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... |