#define REP(i, n) for(int i = 0; i < n; i ++)
#define REPL(i,m, n) for(int i = m; i < n; i ++)
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define SORT(arr) sort(arr.begin(), arr.end())
#define LSOne(S) ((S)&-(S))
#define M_PI 3.1415926535897932384
#define INF 999999999
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef double ld;
void dfs(int u, vvi &adj, vi &parr, vi &harr, int curh) {
harr[u] = curh;
for (auto v: adj[u]) {
if (parr[u] == v) continue;
parr[v] = u;
dfs(v, adj, parr, harr, curh+1);
}
}
int LCA(vi &parr, vi &harr, int u, int v) {
if (u==v) return u;
if (harr[u] < harr[v]) {
return LCA(parr, harr, u,parr[v]);
} else {
return LCA(parr, harr, parr[u], v);
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int n,e;cin>>n>>e;
vii edgearr(e);
map<ii, int> edgemap;
REP(i, e) {
int u,v;cin>>u>>v;u--;v--;
edgearr[i] = {u,v};
edgemap[{u,v}] = i;
edgemap[{v,u}] = i;
}
set<int> st;
vvi adj(n);
REP(i, n-1) {
int k;cin>>k;k--;
st.insert(k);
auto [u,v] = edgearr[k];
adj[u].push_back(v);
adj[v].push_back(u);
}
vi harr(n);
vi parr(n,-1);
dfs(0, adj,parr,harr,0);
/*REP(i, n) {
cout<<parr[i]<<" ";
}
cout<<"\n";
REP(i, n) {
cout<<harr[i]<<" ";
}cout<<"..\n";*/
vi ans(e, -1);
int curlw = 1;
REP(i, e) {
if (ans[i] != -1) continue;
if (st.find(i) != st.end()) {
ans[i] = curlw++;
continue;
}
auto [u,v] = edgearr[i];
int anc = LCA(parr, harr, u,v);
set<int> edges;
while (u!= anc) {
int pr = parr[u];
int edgeno = edgemap[{u, pr}];
if (ans[edgeno] == -1) {
edges.insert(edgeno);
}
parr[u] = anc;
u = pr;
}
while (v != anc) {
int pr = parr[v];
int edgeno = edgemap[{v, pr}];
if (ans[edgeno] == -1) {
edges.insert(edgeno);
}
parr[v] = anc;
v = pr;
}
for (auto kk: edges) {
ans[kk] = curlw++;
}
ans[i] = curlw++;
}
REP(i, e) {
cout<<ans[i]<<" ";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
588 KB |
Output is correct |
6 |
Incorrect |
2 ms |
588 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
209 ms |
26260 KB |
Output is correct |
2 |
Correct |
539 ms |
42420 KB |
Output is correct |
3 |
Correct |
913 ms |
41856 KB |
Output is correct |
4 |
Correct |
685 ms |
70632 KB |
Output is correct |
5 |
Correct |
760 ms |
74572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
441 ms |
46532 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
578 ms |
78896 KB |
Output is correct |
2 |
Correct |
637 ms |
91336 KB |
Output is correct |
3 |
Correct |
120 ms |
25924 KB |
Output is correct |
4 |
Correct |
206 ms |
36300 KB |
Output is correct |
5 |
Correct |
673 ms |
95272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
533 ms |
52360 KB |
Output is correct |
2 |
Correct |
286 ms |
34928 KB |
Output is correct |
3 |
Correct |
872 ms |
77412 KB |
Output is correct |
4 |
Correct |
891 ms |
71884 KB |
Output is correct |
5 |
Correct |
39 ms |
7096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
588 KB |
Output is correct |
6 |
Incorrect |
2 ms |
588 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |