답안 #209755

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
209755 2020-03-15T13:19:12 Z Alexa2001 Rigged Roads (NOI19_riggedroads) C++17
100 / 100
613 ms 55288 KB
#include <bits/stdc++.h>
#define pb push_back

using namespace std;

const int Nmax = 3e5 + 5, lg = 18;

vector<int> v[Nmax];
int x[Nmax], y[Nmax], n, m, tmp, ans[Nmax], val[Nmax], w[Nmax];
int t[lg+2][Nmax];

bool used[Nmax];
int level[Nmax];


struct forest
{
    int t[Nmax];

    void unite(int x, int y)
    {
        t[x] = y;
    }

    int boss(int x)
    {
        if(x == t[x]) return x;
        return t[x] = boss(t[x]);
    }

    void init()
    {
        iota(t+1, t+n+1, 1);
    }
} f;



void dfs(int node, int dad = 0)
{
    t[0][node] = dad;
    level[node] = level[dad] + 1;

    int i;
    for(i=1; i<=lg; ++i)
        t[i][node] = t[i-1][t[i-1][node]];

    for(auto it : v[node])
        if(it != dad)
            dfs(it, node);
}

int lca(int x, int y)
{
    if(level[x] < level[y]) swap(x, y);
    int up = level[x] - level[y], i;

    for(i=0; i<=lg; ++i)
        if(up & (1<<i)) x = t[i][x];

    if(x == y) return x;

    for(i=lg; i>=0; --i)
        if(t[i][x] != t[i][y])
            x = t[i][x], y = t[i][y];
    return t[0][x];
}

int solve(int x, int y, int id)
{
    int l = lca(x, y);
    int nr = 0;

    x = f.boss(x);
    while(level[x] > level[l])
    {
        f.unite(x, t[0][x]);
        ++nr;
        w[x] = id;
        x = f.boss(x);
    }

    y = f.boss(y);
    while(level[y] > level[l])
    {
        f.unite(y, t[0][y]);
        ++nr;
        w[y] = id;
        y = f.boss(y);
    }
    return nr;
}

int main()
{
  //  freopen("input", "r", stdin);
    cin.sync_with_stdio(false); cin.tie(0);

    cin >> n >> m;
    int i;
    for(i=1; i<=m; ++i)
        cin >> x[i] >> y[i];

    for(i=1; i<n; ++i)
    {
        int id;
        cin >> id;
        used[id] = 1;

        v[x[id]].pb(y[id]);
        v[y[id]].pb(x[id]);
    }

    dfs(1);
    f.init();

    int nr = 0;

    for(i=1; i<=m; ++i)
        if(used[i])
        {
            if(level[x[i]] < level[y[i]]) swap(x[i], y[i]);

            if(w[x[i]])
                ans[i] = ++val[w[x[i]]];
            else
            {
                ans[i] = ++nr;
                f.unite(x[i], y[i]);
            }
        }
        else
        {
            val[i] = nr;
            nr += solve(x[i], y[i], i);
            ans[i] = ++nr;
        }

    for(i=1; i<=m; ++i) cout << ans[i] << ' ';

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 7548 KB Output is correct
2 Correct 9 ms 7544 KB Output is correct
3 Correct 10 ms 7544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 7548 KB Output is correct
2 Correct 9 ms 7544 KB Output is correct
3 Correct 10 ms 7544 KB Output is correct
4 Correct 10 ms 7672 KB Output is correct
5 Correct 11 ms 7672 KB Output is correct
6 Correct 10 ms 7672 KB Output is correct
7 Correct 10 ms 7672 KB Output is correct
8 Correct 14 ms 7672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 120 ms 21872 KB Output is correct
2 Correct 146 ms 27376 KB Output is correct
3 Correct 117 ms 16376 KB Output is correct
4 Correct 257 ms 46772 KB Output is correct
5 Correct 197 ms 48740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 183 ms 27976 KB Output is correct
2 Correct 85 ms 16120 KB Output is correct
3 Correct 48 ms 12000 KB Output is correct
4 Correct 106 ms 22584 KB Output is correct
5 Correct 41 ms 13688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 323 ms 46616 KB Output is correct
2 Correct 363 ms 52320 KB Output is correct
3 Correct 87 ms 20216 KB Output is correct
4 Correct 128 ms 26340 KB Output is correct
5 Correct 381 ms 55288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 292 ms 34552 KB Output is correct
2 Correct 176 ms 26300 KB Output is correct
3 Correct 553 ms 49508 KB Output is correct
4 Correct 476 ms 46584 KB Output is correct
5 Correct 31 ms 11256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 7548 KB Output is correct
2 Correct 9 ms 7544 KB Output is correct
3 Correct 10 ms 7544 KB Output is correct
4 Correct 10 ms 7672 KB Output is correct
5 Correct 11 ms 7672 KB Output is correct
6 Correct 10 ms 7672 KB Output is correct
7 Correct 10 ms 7672 KB Output is correct
8 Correct 14 ms 7672 KB Output is correct
9 Correct 120 ms 21872 KB Output is correct
10 Correct 146 ms 27376 KB Output is correct
11 Correct 117 ms 16376 KB Output is correct
12 Correct 257 ms 46772 KB Output is correct
13 Correct 197 ms 48740 KB Output is correct
14 Correct 183 ms 27976 KB Output is correct
15 Correct 85 ms 16120 KB Output is correct
16 Correct 48 ms 12000 KB Output is correct
17 Correct 106 ms 22584 KB Output is correct
18 Correct 41 ms 13688 KB Output is correct
19 Correct 323 ms 46616 KB Output is correct
20 Correct 363 ms 52320 KB Output is correct
21 Correct 87 ms 20216 KB Output is correct
22 Correct 128 ms 26340 KB Output is correct
23 Correct 381 ms 55288 KB Output is correct
24 Correct 292 ms 34552 KB Output is correct
25 Correct 176 ms 26300 KB Output is correct
26 Correct 553 ms 49508 KB Output is correct
27 Correct 476 ms 46584 KB Output is correct
28 Correct 31 ms 11256 KB Output is correct
29 Correct 573 ms 45944 KB Output is correct
30 Correct 613 ms 47720 KB Output is correct
31 Correct 526 ms 47096 KB Output is correct
32 Correct 131 ms 16120 KB Output is correct
33 Correct 526 ms 47480 KB Output is correct