Submission #248681

# Submission time Handle Problem Language Result Execution time Memory
248681 2020-07-13T06:43:28 Z SamAnd Rigged Roads (NOI19_riggedroads) C++17
100 / 100
688 ms 82872 KB
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 300005;

int n, m;
pair<int, int> b[N];
int u[N];

vector<int> t[N], ti[N];

const int k = 19;
int p[N][k];
int k0[N];
int tin[N], tout[N], tim;

void dfs0(int x, int p0)
{
    tin[x] = ++tim;
    p[x][0] = p0;
    for (int i = 1; i < k; ++i)
    {
        p[x][i] = p[p[x][i - 1]][i - 1];
    }
    for (int i = 0; i < t[x].size(); ++i)
    {
        int h = t[x][i];
        int hi = ti[x][i];
        if (h == p0)
            continue;
        k0[h] = hi;
        dfs0(h, x);
    }
    tout[x] = tim;
}

bool isp(int x, int y)
{
    return (tin[x] <= tin[y] && tout[y] <= tout[x]);
}

int lca(int x, int y)
{
    if (isp(x, y))
        return x;
    if (isp(y, x))
        return y;
    for (int i = k - 1; i >= 0; --i)
    {
        if (!isp(p[x][i], y))
            x = p[x][i];
    }
    return p[x][0];
}

int p0[N];
int fi(int x)
{
    if (x == p0[x])
        return x;
    return p0[x] = fi(p0[x]);
}

int z;
int ans[N];

void solv()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; ++i)
    {
        scanf("%d%d", &b[i].fi, &b[i].se);
    }
    for (int i = 1; i <= n - 1; ++i)
    {
        scanf("%d", &u[i]);
        t[b[u[i]].fi].push_back(b[u[i]].se);
        t[b[u[i]].se].push_back(b[u[i]].fi);
        ti[b[u[i]].fi].push_back(u[i]);
        ti[b[u[i]].se].push_back(u[i]);
    }
    sort(u + 1, u + n);
    dfs0(1, 1);
    for (int x = 1; x <= n; ++x)
        p0[x] = x;

    for (int i = 1; i <= m; ++i)
    {
        int x = b[i].fi;
        int y = b[i].se;

        if (binary_search(u + 1, u + n, i))
        {
            if (isp(x, y))
                swap(x, y);
            if (fi(x) != x)
                continue;
            p0[x] = y;
            ans[k0[x]] = ++z;
            continue;
        }

        int l = lca(x, y);

        vector<int> v;
        while (1)
        {
            x = fi(x);
            if (isp(x, l))
                break;
            p0[x] = p[x][0];
            v.push_back(k0[x]);
        }

        swap(x, y);
        while (1)
        {
            x = fi(x);
            if (isp(x, l))
                break;
            p0[x] = p[x][0];
            v.push_back(k0[x]);
        }

        sort(all(v));
        for (int i = 0; i < v.size(); ++i)
            ans[v[i]] = ++z;

        ans[i] = ++z;
    }
    for (int i = 1; i <= m; ++i)
        printf("%d ", ans[i]);
    printf("\n");
}

int main()
{
    #ifdef SOMETHING
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    #endif // SOMETHING
    solv();
    return 0;
}

//while ((double)clock() / CLOCKS_PER_SEC <= 0.9){}

Compilation message

riggedroads.cpp: In function 'void dfs0(int, int)':
riggedroads.cpp:32:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < t[x].size(); ++i)
                     ~~^~~~~~~~~~~~~
riggedroads.cpp: In function 'void solv()':
riggedroads.cpp:133:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int i = 0; i < v.size(); ++i)
                         ~~^~~~~~~~~~
riggedroads.cpp:76:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &n, &m);
     ~~~~~^~~~~~~~~~~~~~~~
riggedroads.cpp:79:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &b[i].fi, &b[i].se);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
riggedroads.cpp:83:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &u[i]);
         ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14592 KB Output is correct
2 Correct 9 ms 14464 KB Output is correct
3 Correct 9 ms 14464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14592 KB Output is correct
2 Correct 9 ms 14464 KB Output is correct
3 Correct 9 ms 14464 KB Output is correct
4 Correct 10 ms 14592 KB Output is correct
5 Correct 10 ms 14592 KB Output is correct
6 Correct 9 ms 14592 KB Output is correct
7 Correct 9 ms 14592 KB Output is correct
8 Correct 13 ms 14592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 98 ms 33764 KB Output is correct
2 Correct 190 ms 41404 KB Output is correct
3 Correct 164 ms 25848 KB Output is correct
4 Correct 267 ms 68308 KB Output is correct
5 Correct 291 ms 70732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 152 ms 42396 KB Output is correct
2 Correct 90 ms 26100 KB Output is correct
3 Correct 48 ms 20472 KB Output is correct
4 Correct 118 ms 35044 KB Output is correct
5 Correct 57 ms 22904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 467 ms 70116 KB Output is correct
2 Correct 486 ms 77756 KB Output is correct
3 Correct 105 ms 32280 KB Output is correct
4 Correct 169 ms 41228 KB Output is correct
5 Correct 541 ms 82872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 359 ms 52312 KB Output is correct
2 Correct 214 ms 40696 KB Output is correct
3 Correct 640 ms 74360 KB Output is correct
4 Correct 587 ms 69624 KB Output is correct
5 Correct 40 ms 19704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14592 KB Output is correct
2 Correct 9 ms 14464 KB Output is correct
3 Correct 9 ms 14464 KB Output is correct
4 Correct 10 ms 14592 KB Output is correct
5 Correct 10 ms 14592 KB Output is correct
6 Correct 9 ms 14592 KB Output is correct
7 Correct 9 ms 14592 KB Output is correct
8 Correct 13 ms 14592 KB Output is correct
9 Correct 98 ms 33764 KB Output is correct
10 Correct 190 ms 41404 KB Output is correct
11 Correct 164 ms 25848 KB Output is correct
12 Correct 267 ms 68308 KB Output is correct
13 Correct 291 ms 70732 KB Output is correct
14 Correct 152 ms 42396 KB Output is correct
15 Correct 90 ms 26100 KB Output is correct
16 Correct 48 ms 20472 KB Output is correct
17 Correct 118 ms 35044 KB Output is correct
18 Correct 57 ms 22904 KB Output is correct
19 Correct 467 ms 70116 KB Output is correct
20 Correct 486 ms 77756 KB Output is correct
21 Correct 105 ms 32280 KB Output is correct
22 Correct 169 ms 41228 KB Output is correct
23 Correct 541 ms 82872 KB Output is correct
24 Correct 359 ms 52312 KB Output is correct
25 Correct 214 ms 40696 KB Output is correct
26 Correct 640 ms 74360 KB Output is correct
27 Correct 587 ms 69624 KB Output is correct
28 Correct 40 ms 19704 KB Output is correct
29 Correct 639 ms 67384 KB Output is correct
30 Correct 688 ms 69960 KB Output is correct
31 Correct 650 ms 69408 KB Output is correct
32 Correct 160 ms 25724 KB Output is correct
33 Correct 638 ms 69824 KB Output is correct