#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mp(x, y) make_pair(x, y)
#define sz(v) ((int) (v).size())
#define all(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define BIT(x, y) (((x) >> (y)) & 1)
#define clz __builtin_clzll
#define ctz __builtin_ctzll
#define popcount __builtin_popcount
#define lg(x) (63 - clz(x))
template <class X, class Y>
inline bool maximize(X &x, Y y) {
return (x < y ? x = y, true : false);
}
template <class X, class Y>
inline bool minimize(X &x, Y y) {
return (x > y ? x = y, true : false);
}
template <class X>
inline void compress(vector<X> &a) {
sort(all(a)); a.resize(unique(all(a)) - a.begin());
}
const ll oo = (ll) 1e18, inf = (ll) 1e9, mod = (ll) 1e9 + 7;
const int mxn = (int) 2e5 + 5, S = (int) 450, S2 = (int) 450, lg = (int) 17;
void add(ll &x, ll y) {
x += y;
if (x >= mod) x -= mod;
}
void sub(ll &x, ll y) {
x -= y;
if (x < 0) x += mod;
}
struct Edge {
int u, v;
void input() {
cin >> u >> v;
}
};
int n, e;
bool pos[mxn];
Edge edges[mxn];
namespace HLD {
vector<int> g[mxn];
struct Segtree {
int n;
vector<int> st, lz;
Segtree(int n) : n(n) {
int m = MASK(lg(n));
st.resize(m << 2);
lz.resize(m << 2, -1);
}
void upd(int i, int l, int r, int u, int v, int x) {
if (l > v || r < u) return;
if (l >= u && r <= v) {
}
}
};
int h[mxn];
int par[mxn][lg + 1];
int id[mxn];
void dfs(int u = 1, int p = 0) {
for (int i = 1; i <= lg; ++i)
par[u][i] = par[par[u][i-1]][i-1];
for (int v : g[u]) if (v != p) {
h[v] = h[u] + 1;
par[v][0] = u;
dfs(v, u);
}
return;
}
int find_ancestor(int u, int k) {
for (; k; k -= k & -k)
u = par[u][ctz(k)];
return u;
}
int lca(int u, int v) {
if (h[u] < h[v])
swap(u, v);
u = find_ancestor(u, h[u] - h[v]);
if (u == v) return u;
for (int i = lg(h[u]); i >= 0; --i) if (par[u][i] != par[v][i]) {
u = par[u][i]; v = par[v][i];
}
return par[u][0];
}
int value[mxn];
vector<int> nodes;
void getPath(int u, int v) {
nodes.clear();
int p = lca(u, v);
// int ans = 0;
while (u != p) {
if (!value[u]) nodes.push_back(id[u]);
u = par[u][0];
}
while (v != p) {
if (!value[v]) nodes.push_back(id[v]);
v = par[v][0];
}
sort(all(nodes));
}
void updPath(int u, int v, int x) {
int p = lca(u, v);
// cout << "UPD " << u << ' ' << v << ' ' << p << ' ' << x << '\n';
// value[p] = 0;
while (u != p) {
value[u] = x;
u = par[u][0];
}
while (v != p) {
value[v] = x;
v = par[v][0];
}
}
// int getEmpt(int u, int v) {
// // cout << u << ' ' << v << ' ' << h[u] + h[v] - 2 * h[lca(u, v)] << ' ' << getPath(u, v) << '\n';
// return h[u] + h[v] - 2 * h[lca(u, v)] - getPath(u, v);
// }
void build() {
for (int i = 1; i <= e; ++i) if (pos[i]) {
int u = edges[i].u, v = edges[i].v;
g[u].push_back(v); g[v].push_back(u);
}
dfs();
for (int i = 1; i <= e; ++i) if (pos[i]) {
int u = edges[i].u, v = edges[i].v;
if (par[u][0] != v) swap(u, v);
id[u] = i;
}
return;
}
}
void solve() {
cin >> n >> e;
for (int i = 1; i <= e; ++i)
edges[i].input();
for (int i = 1; i < n; ++i) {
int p; cin >> p;
pos[p] = true;
}
HLD::build();
set<int> s;
for (int i = 1; i <= e; ++i)
s.insert(i);
int cnt = 0;
vector<int> ans(e+1);
vector<int> assigned(e+1);
for (int i = 1; i <= e; ++i) {
int u = edges[i].u, v = edges[i].v;
// cout << "PHASE i = " << i << "\n";
// for (int i = 1; i <= e; ++i) if (pos[i])
// cout << HLD::value[edges[i].v] << ' ';
// cout << '\n';
// cout << u << ' ' << v << '\n';
if (pos[i]) {
// cout <<
// cout << i << ' ' << HLD::getPath(v, v) << '\n';
if (ans[i]) continue;
ans[i] = ++cnt;
HLD::updPath(u, v, 1);
}
else {
// cout << "THREE\n";
HLD::getPath(u, v);
for (int num : HLD::nodes) {
ans[num] = ++cnt;
}
ans[i] = ++cnt;
HLD::updPath(u, v, 1);
}
// cout << "PHASE END:\n\n";
}
for (int i = 1; i <= e; ++i)
cout << ans[i] << ' ';
return;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define TASK "task"
if (fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}
Compilation message
riggedroads.cpp: In function 'int main()':
riggedroads.cpp:225:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
225 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
riggedroads.cpp:226:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
226 | freopen(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8792 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
29388 KB |
Output is correct |
2 |
Correct |
79 ms |
39624 KB |
Output is correct |
3 |
Runtime error |
481 ms |
262144 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2036 ms |
39128 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
26 ms |
20564 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
46288 KB |
Output is correct |
2 |
Correct |
62 ms |
35912 KB |
Output is correct |
3 |
Runtime error |
26 ms |
20564 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8792 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8796 KB |
Output is correct |
9 |
Correct |
48 ms |
29388 KB |
Output is correct |
10 |
Correct |
79 ms |
39624 KB |
Output is correct |
11 |
Runtime error |
481 ms |
262144 KB |
Execution killed with signal 9 |
12 |
Halted |
0 ms |
0 KB |
- |