Submission #172314

# Submission time Handle Problem Language Result Execution time Memory
172314 2020-01-01T07:05:14 Z qkxwsm Simurgh (IOI17_simurgh) C++14
70 / 100
213 ms 6140 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include "simurgh.h"

using namespace std;
using namespace __gnu_pbds;

template<class T, class U>
void ckmin(T &a, U b)
{
    if (a > b) a = b;
}

template<class T, class U>
void ckmax(T &a, U b)
{
    if (a < b) a = b;
}

struct custom_hash
{
    template<class T>
    T operator()(T a) const
    {
        return (a ^ (a >> 15)) ^ (a * 69);
    }
};

template<class T, class U>
using hash_table = gp_hash_table<T, U, custom_hash>;

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define MAXN 513

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;

int N, M, T;
int eid[MAXN][MAXN];
int st[MAXN], ft[MAXN];
hash_table<int, int> rec[MAXN];
vi edge[MAXN];
bitset<MAXN> seen;
int parent[MAXN], depth[MAXN];
int ans[MAXN * MAXN];
vi quer;
vi ret;

int get(int a, int b)
{
    if (rec[a].find(b) != rec[a].end()) return rec[a][b];
    quer.clear();
    if (a == 0 && b == 0)
    {
        FOR(i, 1, N)
        {
            quer.PB(eid[i][parent[i]]);
        }
        rec[a][b] = count_common_roads(quer);
    }
    else
    {
        FOR(i, 1, N)
        {
            if (i == a) continue;
            quer.PB(eid[i][parent[i]]);
        }
        quer.PB(b);
        rec[a][b] = count_common_roads(quer);
    }
    return rec[a][b];
    //take the tree, except delete a and add edge b.
}

void dfs(int u, int p)
{
    st[u] = T; ft[u] = T;
    T++;
    seen[u] = true;
    for (int v : edge[u])
    {
        if (v == p) continue;
        if (seen[v])
        {
            ckmin(ft[u], st[v]);
            continue;
        }
        parent[v] = u;
        depth[v] = depth[u] + 1;
        dfs(v, u);
        ckmin(ft[u], ft[v]);
    }
}

vi find_roads(int n, vi e1, vi e2)
{
    N = n; M = SZ(e1);
    FOR(i, 0, M)
    {
        edge[e1[i]].PB(e2[i]);
        edge[e2[i]].PB(e1[i]);
        eid[e1[i]][e2[i]] = i;
        eid[e2[i]][e1[i]] = i;
        ans[i] = -1;
    }
    if (N >= 3 && M == N * (N - 1) / 2)
    {
        FOR(i, 0, M) ans[i] = 0;
        int R = -1;
        FOR(i, 0, N)
        {
            quer.clear();
            FOR(j, 0, N)
            {
                if (i == j) continue;
                quer.PB(eid[i][j]);
            }
            depth[i] = count_common_roads(quer);
            // cerr << depth[i] << ' ';
        }
        // cerr << endl;
        FOR(i, 0, N)
        {
            if (depth[i] == 1)
            {
                R = i;
                break;
            }
        }
        // cerr << "R = " << R << endl;
        //R is a leaf.
        //for each of the N edges connected to R, check to see if it's the on one.
        FOR(i, 0, N)
        {
            if (i == R) continue;
            quer.clear();
            int s = 0;
            while(s == R || s == i) s++;
            FOR(j, 0, N)
            {
                if (j == s || j == R || j == i) continue;
                quer.PB(eid[s][j]);
            }
            quer.PB(eid[R][s]);
            quer.PB(eid[R][i]);
            // cerr << "sz " << SZ(quer) << endl;
            int si = count_common_roads(quer);
            quer.pop_back();
            quer.PB(eid[s][i]);
            int ri = count_common_roads(quer);
            quer.pop_back();
            quer.pop_back();
            quer.PB(eid[s][i]);
            quer.PB(eid[R][i]);
            int rs = count_common_roads(quer);
            if (ri < rs || ri < si)
            {
                T = i;
                break;
            }
            //is R...i on?
        }
        // cerr << "T = " << T << endl;
        // cerr << "ALIVE\n";
        ans[eid[R][T]] = 1;
        FOR(i, 0, N)
        {
            if (i == R) continue;
            int lo = -1, hi;
            vi idk;
            // cerr << "OK " << i << endl;
            FOR(j, 0, N)
            {
                if (j == i || j == R) continue;
                idk.PB(j);
                // cerr << j << ' ';
            }
            // cerr << endl;
            // cerr << "OK " << i << endl;
            int num = depth[i]; if (i == T) num--;
            FOR(j, 0, num)
            {
                lo++; hi = SZ(idk) - 1;
                while(hi > lo)
                {
                    int mid = (hi + lo) >> 1;
                    quer.clear();
                    FOR(k, 0, mid + 1)
                    {
                        quer.PB(eid[idk[k]][i]);
                    }
                    FOR(k, mid + 1, SZ(idk))
                    {
                        quer.PB(eid[idk[k]][R]);
                    }
                    quer.PB(eid[i][R]);
                    // cerr << SZ(quer) << endl;
                    int c = count_common_roads(quer);
                    FOR(k, mid + 1, SZ(idk))
                    {
                        if (idk[k] == T) c--;
                    }
                    if (i == T) c--;
                    if (c >= j + 1) hi = mid;
                    else lo = mid + 1;
                }
                ans[eid[i][idk[lo]]] = 1;
            }
        }
    }
    else
    {
        parent[0] = N; parent[N] = N;
        dfs(0, N);
        // FOR(i, 1, N)
        // {
        //     cerr << i << " -> " << parent[i] << endl;
        // }
        //1 = yes, -1 = no, 0 = idk
        FOR(ed, 0, M)
        {
            int u = e1[ed], v = e2[ed];
            if (depth[u] > depth[v]) swap(u, v);
            if (u == parent[v]) continue;
            //lol there are no side edges.
            vi nodes; nodes.PB(v);
            while(nodes.back() != u)
            {
                nodes.PB(parent[nodes.back()]);
            }
            assert(nodes.back() == u);
            //you know the answer?
            int s = -1;
            FOR(i, 0, SZ(nodes) - 1)
            {
                if (ans[eid[nodes[i]][nodes[i + 1]]] != -1)
                {
                    s = nodes[i];
                    break;
                }
            }
            if (s == -1)
            {
                int mn = get(0, 0), mx = mn;
                FOR(i, 0, SZ(nodes) - 1)
                {
                    int x = get(nodes[i], ed);
                    ckmin(mn, x);
                    ckmax(mx, x);
                }
                FOR(i, 0, SZ(nodes) - 1)
                {
                    int x = get(nodes[i], ed);
                    ans[eid[nodes[i]][nodes[i + 1]]] = ((mn == mx || x == mx) ? 0 : 1);
                }
                int x = get(0, 0);
                ans[ed] = ((mn == mx || x == mx) ? 0 : 1);
            }
            else
            {
                int x = get(s, ed);
                int y = get(0, 0);
                //y + ed - s = x -> ed = x + s - y
                ans[ed] = x + ans[eid[s][parent[s]]] - y;
                FOR(i, 0, SZ(nodes) - 1)
                {
                    int ed1 = eid[nodes[i]][nodes[i + 1]];
                    if (ans[ed1] != -1) continue;
                    x = get(nodes[i], ed);
                    //x = y + ed - ed1
                    ans[ed1] = y - x + ans[ed];
                }
            }
        }
        FOR(u, 1, N)
        {
            if (ans[eid[u][parent[u]]] == -1)
            {
                ans[eid[u][parent[u]]] = 1;
            }
        }
    }
    // cerr << "HELLO\n";
    FOR(i, 0, M)
    {
        assert(ans[i] == 0 || ans[i] == 1);
        if (ans[i] == 1) ret.PB(i);
    }
    // cerr << "RETURNS:";
    // for (int x : ret)
    // {
    //     cerr << ' ' << x;
    // }
    // cerr << endl;
    return ret;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB correct
2 Correct 2 ms 504 KB correct
3 Correct 2 ms 504 KB correct
4 Correct 2 ms 504 KB correct
5 Correct 2 ms 504 KB correct
6 Correct 2 ms 504 KB correct
7 Correct 2 ms 376 KB correct
8 Correct 2 ms 376 KB correct
9 Correct 2 ms 504 KB correct
10 Correct 2 ms 504 KB correct
11 Correct 2 ms 508 KB correct
12 Correct 2 ms 504 KB correct
13 Correct 2 ms 504 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB correct
2 Correct 2 ms 504 KB correct
3 Correct 2 ms 504 KB correct
4 Correct 2 ms 504 KB correct
5 Correct 2 ms 504 KB correct
6 Correct 2 ms 504 KB correct
7 Correct 2 ms 376 KB correct
8 Correct 2 ms 376 KB correct
9 Correct 2 ms 504 KB correct
10 Correct 2 ms 504 KB correct
11 Correct 2 ms 508 KB correct
12 Correct 2 ms 504 KB correct
13 Correct 2 ms 504 KB correct
14 Correct 3 ms 632 KB correct
15 Correct 3 ms 632 KB correct
16 Correct 3 ms 632 KB correct
17 Correct 4 ms 632 KB correct
18 Correct 3 ms 632 KB correct
19 Correct 4 ms 632 KB correct
20 Correct 4 ms 632 KB correct
21 Correct 4 ms 632 KB correct
22 Correct 3 ms 632 KB correct
23 Correct 3 ms 632 KB correct
24 Correct 3 ms 632 KB correct
25 Correct 2 ms 632 KB correct
26 Correct 3 ms 632 KB correct
27 Correct 4 ms 632 KB correct
28 Correct 3 ms 504 KB correct
29 Correct 2 ms 632 KB correct
30 Correct 3 ms 632 KB correct
31 Correct 3 ms 632 KB correct
32 Correct 3 ms 632 KB correct
33 Correct 3 ms 632 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB correct
2 Correct 2 ms 504 KB correct
3 Correct 2 ms 504 KB correct
4 Correct 2 ms 504 KB correct
5 Correct 2 ms 504 KB correct
6 Correct 2 ms 504 KB correct
7 Correct 2 ms 376 KB correct
8 Correct 2 ms 376 KB correct
9 Correct 2 ms 504 KB correct
10 Correct 2 ms 504 KB correct
11 Correct 2 ms 508 KB correct
12 Correct 2 ms 504 KB correct
13 Correct 2 ms 504 KB correct
14 Correct 3 ms 632 KB correct
15 Correct 3 ms 632 KB correct
16 Correct 3 ms 632 KB correct
17 Correct 4 ms 632 KB correct
18 Correct 3 ms 632 KB correct
19 Correct 4 ms 632 KB correct
20 Correct 4 ms 632 KB correct
21 Correct 4 ms 632 KB correct
22 Correct 3 ms 632 KB correct
23 Correct 3 ms 632 KB correct
24 Correct 3 ms 632 KB correct
25 Correct 2 ms 632 KB correct
26 Correct 3 ms 632 KB correct
27 Correct 4 ms 632 KB correct
28 Correct 3 ms 504 KB correct
29 Correct 2 ms 632 KB correct
30 Correct 3 ms 632 KB correct
31 Correct 3 ms 632 KB correct
32 Correct 3 ms 632 KB correct
33 Correct 3 ms 632 KB correct
34 Correct 37 ms 1784 KB correct
35 Correct 187 ms 2780 KB correct
36 Correct 127 ms 2296 KB correct
37 Correct 10 ms 1016 KB correct
38 Correct 191 ms 2936 KB correct
39 Correct 162 ms 2588 KB correct
40 Correct 129 ms 2424 KB correct
41 Correct 39 ms 1784 KB correct
42 Correct 193 ms 2808 KB correct
43 Correct 99 ms 2260 KB correct
44 Correct 80 ms 1704 KB correct
45 Correct 93 ms 1784 KB correct
46 Correct 69 ms 1656 KB correct
47 Correct 31 ms 1272 KB correct
48 Correct 5 ms 1016 KB correct
49 Correct 11 ms 1016 KB correct
50 Correct 33 ms 1272 KB correct
51 Correct 92 ms 1916 KB correct
52 Correct 79 ms 1784 KB correct
53 Correct 71 ms 1672 KB correct
54 Correct 100 ms 2040 KB correct
55 Correct 97 ms 1784 KB correct
56 Correct 96 ms 1784 KB correct
57 Correct 97 ms 2040 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB correct
2 Correct 3 ms 504 KB correct
3 Correct 123 ms 3832 KB correct
4 Correct 191 ms 6016 KB correct
5 Correct 199 ms 6136 KB correct
6 Correct 204 ms 6040 KB correct
7 Correct 193 ms 6008 KB correct
8 Correct 193 ms 6008 KB correct
9 Correct 208 ms 6108 KB correct
10 Correct 206 ms 6056 KB correct
11 Correct 200 ms 6008 KB correct
12 Correct 213 ms 6072 KB correct
13 Correct 2 ms 504 KB correct
14 Correct 203 ms 6140 KB correct
15 Correct 195 ms 6008 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB correct
2 Correct 2 ms 504 KB correct
3 Correct 2 ms 504 KB correct
4 Correct 2 ms 504 KB correct
5 Correct 2 ms 504 KB correct
6 Correct 2 ms 504 KB correct
7 Correct 2 ms 376 KB correct
8 Correct 2 ms 376 KB correct
9 Correct 2 ms 504 KB correct
10 Correct 2 ms 504 KB correct
11 Correct 2 ms 508 KB correct
12 Correct 2 ms 504 KB correct
13 Correct 2 ms 504 KB correct
14 Correct 3 ms 632 KB correct
15 Correct 3 ms 632 KB correct
16 Correct 3 ms 632 KB correct
17 Correct 4 ms 632 KB correct
18 Correct 3 ms 632 KB correct
19 Correct 4 ms 632 KB correct
20 Correct 4 ms 632 KB correct
21 Correct 4 ms 632 KB correct
22 Correct 3 ms 632 KB correct
23 Correct 3 ms 632 KB correct
24 Correct 3 ms 632 KB correct
25 Correct 2 ms 632 KB correct
26 Correct 3 ms 632 KB correct
27 Correct 4 ms 632 KB correct
28 Correct 3 ms 504 KB correct
29 Correct 2 ms 632 KB correct
30 Correct 3 ms 632 KB correct
31 Correct 3 ms 632 KB correct
32 Correct 3 ms 632 KB correct
33 Correct 3 ms 632 KB correct
34 Correct 37 ms 1784 KB correct
35 Correct 187 ms 2780 KB correct
36 Correct 127 ms 2296 KB correct
37 Correct 10 ms 1016 KB correct
38 Correct 191 ms 2936 KB correct
39 Correct 162 ms 2588 KB correct
40 Correct 129 ms 2424 KB correct
41 Correct 39 ms 1784 KB correct
42 Correct 193 ms 2808 KB correct
43 Correct 99 ms 2260 KB correct
44 Correct 80 ms 1704 KB correct
45 Correct 93 ms 1784 KB correct
46 Correct 69 ms 1656 KB correct
47 Correct 31 ms 1272 KB correct
48 Correct 5 ms 1016 KB correct
49 Correct 11 ms 1016 KB correct
50 Correct 33 ms 1272 KB correct
51 Correct 92 ms 1916 KB correct
52 Correct 79 ms 1784 KB correct
53 Correct 71 ms 1672 KB correct
54 Correct 100 ms 2040 KB correct
55 Correct 97 ms 1784 KB correct
56 Correct 96 ms 1784 KB correct
57 Correct 97 ms 2040 KB correct
58 Correct 2 ms 504 KB correct
59 Correct 3 ms 504 KB correct
60 Correct 123 ms 3832 KB correct
61 Correct 191 ms 6016 KB correct
62 Correct 199 ms 6136 KB correct
63 Correct 204 ms 6040 KB correct
64 Correct 193 ms 6008 KB correct
65 Correct 193 ms 6008 KB correct
66 Correct 208 ms 6108 KB correct
67 Correct 206 ms 6056 KB correct
68 Correct 200 ms 6008 KB correct
69 Correct 213 ms 6072 KB correct
70 Correct 2 ms 504 KB correct
71 Correct 203 ms 6140 KB correct
72 Correct 195 ms 6008 KB correct
73 Correct 2 ms 504 KB correct
74 Incorrect 172 ms 6008 KB WA in grader: NO
75 Halted 0 ms 0 KB -