This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std ;
const int MAX = 3e5 + 10 ;
int arr[MAX] ;
int n , m ;
int u[MAX] , v[MAX] ;
vector< vector< pair<int , int> > >adj(MAX) ;
int par[MAX] ;
void init()
{
for(int i = 1 ; i <= n ; ++i)
par[i] = i ;
}
int root(int node)
{
if(par[node] == node)
return node ;
return (par[node] = root(par[node])) ;
}
void Union(int x , int y)
{
int a = root(x) ;
int b = root(y) ;
par[a] = b ;
}
int P[MAX] , dep[MAX] , id[MAX] ;
void dfs(int node , int par)
{
P[node] = par ;
for(auto &child : adj[node])
{
if(child.first == par)
continue ;
dep[child.first] = dep[node] + 1 , id[child.first] = child.second ;
dfs(child.first , node) ;
}
}
int ans[MAX] ;
vector<int>edges ;
int now = 1 ;
void solve(int x , int y)
{
edges.clear() ;
x = root(x) , y = root(y) ;
while(x != y)
{
if(dep[x] < dep[y])
swap(x , y) ;
edges.push_back(id[x]) ;
Union(x , P[x]) ;
x = root(x) ;
}
sort(edges.begin() , edges.end()) ;
for(auto &i : edges)
ans[i] = now , ++now ;
}
int main()
{
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>n>>m ;
for(int i = 1 ; i <= m ; ++i)
cin>>u[i]>>v[i] ;
for(int i = 0 ; i < n-1 ; ++i)
{
cin>>arr[i] ;
adj[u[arr[i]]].emplace_back(v[arr[i]] , arr[i]) ;
adj[v[arr[i]]].emplace_back(u[arr[i]] , arr[i]) ;
}
dfs(1 , -1) ;
init() ;
for(int i = 1 ; i <= m ; ++i)
{
if(ans[i])
continue ;
solve(u[i] , v[i]) ;
if(!ans[i])
ans[i] = now , now++ ;
}
for(int i = 1 ; i <= m ; ++i)
cout<<ans[i]<<" " ;
cout<<"\n" ;
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... |