#include <bits/stdc++.h>
#define el '\n'
#define FNAME "NOI19_riggedroads"
#define allof(x) x.begin(),x.end()
#define allof1(x) x.begin()+1,x.end()
#define mset(x,n) memset(x,(n),sizeof(x))
using namespace std;
const long long MOD = (long long) 1e9+7;
template<class X,class Y> bool minimize(X &a,Y b){ if (a>b) {a=b; return true;} return false;}
template<class X,class Y> bool maximize(X &a,Y b){ if (a<b) {a=b; return true;} return false;}
void setup(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
if (fopen(FNAME".inp","r")){
freopen(FNAME".inp","r",stdin);
freopen(FNAME".out","w",stdout);
}
}
const int MAXN = 3e5 + 5;
struct Edges{
int v,ID;
Edges(): v(0), ID(-1) {}
Edges(int V,int id): v(V), ID(id) {}
};
int n,m;
vector<Edges> graph[MAXN];
bool inMST[MAXN];
int eID[MAXN];
vector<pair<int,int>> edges;
void init(){
cin>>n>>m;
for (int i=0;i<m;i++){
int u,v;
cin>>u>>v;
edges.emplace_back(u,v);
}
for (int i=1;i<n;i++){
int e; cin>>e; e--;
inMST[e]=1;
int u,v; tie(u,v) = edges[e];
graph[u].emplace_back(v,e);
graph[v].emplace_back(u,e);
}
}
int depth[MAXN],parent[MAXN];
bool visited[MAXN];
void dfs(int u,int pre){
visited[u] = 1;
parent[u] = pre;
for (Edges x : graph[u]){
int v = x.v;
int ID = x.ID;
if (!visited[v]){
depth[v] = depth[u] + 1;
eID[v] = ID;
dfs(v,u);
}
}
}
namespace DSU{
vector<int> parent,power,rep;
void make_set(){
parent.resize(n+1,0);
power.resize(n+1,1);
rep.assign(n+1,0);
for (int i=1;i<=n;i++){
parent[i]=i;
power[i]=1;
rep[i] = i;
}
}
int find(int u){
if (u==parent[u]) return u;
return parent[u]=find(parent[u]);
}
bool Union(int u,int v){
u=find(u);
v=find(v);
if (u==v) return false;
if (power[u]<power[v]) swap(u,v);
parent[v]=u;
power[u]+=power[v];
if (depth[rep[u]] > depth[rep[v]])
rep[u] = rep[v];
return true;
}
int getRep(int u){
return rep[find(u)];
}
};
void sol(){
dfs(1,1);
DSU::make_set();
int cur=0;
vector<int> res(m,0);
for (int i=0;i<m;i++){
int u,v; tie(u,v) = edges[i];
if (inMST[i]){
if (!res[i]){
res[i] = ++cur;
DSU::Union(u,v);
}
} else{
u = DSU::getRep(u);
v = DSU::getRep(v);
vector<int> indices;
while (u ^ v){
if (depth[u] < depth[v]) swap(u,v);
indices.push_back(eID[u]);
DSU::Union(u,parent[u]);
u = DSU::getRep(u);
}
sort(allof(indices));
for (int x : indices) res[x]=++cur;
res[i] = ++cur;
}
}
for (int x : res) cout<<x<<" ";
}
int main(){
setup();
init();
sol();
}
Compilation message (stderr)
riggedroads.cpp: In function 'void setup()':
riggedroads.cpp:16:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | freopen(FNAME".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
riggedroads.cpp:17:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | freopen(FNAME".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |