Submission #293696

# Submission time Handle Problem Language Result Execution time Memory
293696 2020-09-08T09:57:44 Z Kastanda Simurgh (IOI17_simurgh) C++11
100 / 100
277 ms 7544 KB
// M
#include<bits/stdc++.h>
#include "simurgh.h"
using namespace std;
const int N = 505, MXM = N * N / 2;
int n, m, A[MXM], B[MXM], Stat[MXM];
int P[N], PE[N], H[N], F[N], M[N], Back[MXM];
vector < pair < int , int > > Adj[N];
void DFS(int v, int p, int pe)
{
        M[v] = 1;
        P[v] = p;
        PE[v] = pe;
        for (auto e : Adj[v])
        {
                int u = e.first;
                int id = e.second;
                if (id == pe)
                        continue;
                if (!M[u])
                {
                        H[u] = H[v] + 1;
                        DFS(u, v, id);
                }
                else if (M[u] == 1)
                {
                        int nw = v;
                        while (nw != u)
                        {
                                if (Back[PE[nw]] == -1)
                                        Back[PE[nw]] = id;
                                nw = P[nw];
                        }
                }
        }
        M[v] = 2;
}
int Find(int v)
{
        return (F[v] < 0 ? v : (F[v] = Find(F[v])));
}
inline int Merge(int v, int u)
{
        v = Find(v);
        u = Find(u);
        if (v == u)
                return 0;
        F[v] = u;
        return 1;
}

vector < int > find_roads(int _n, vector < int > _u, vector < int > _v)
{
        n = _n;
        m = (int)_u.size();
        for (int i = 0; i < m; i ++)
                A[i] = _u[i], B[i] = _v[i];

        for (int i = 0; i < m; i ++)
        {
                Adj[A[i]].push_back({B[i], i});
                Adj[B[i]].push_back({A[i], i});
        }

        memset(Stat, -1, sizeof(Stat));
        memset(Back, -1, sizeof(Back));
        DFS(0, -1, -1);

        for (int i = 1; i < n; i ++)
                if (Stat[PE[i]] == -1)
                {
                        int e = Back[PE[i]];
                        if (e == -1)
                        {
                                Stat[PE[i]] = 1;
                                continue;
                        }
                        int v = A[e], u = B[e];
                        if (H[v] < H[u])
                                swap(v, u);
                        int nw = v;
                        vector < int > V, R;
                        memset(M, 0, sizeof(M));
                        while (nw != u)
                        {
                                if (Stat[PE[nw]] == -1)
                                        V.push_back(PE[nw]);
                                else
                                        R.push_back(PE[nw]);
                                M[nw] = 1;
                                nw = P[nw];
                        }
                        assert((int)V.size());
                        V.push_back(e);
                        vector < int > T;
                        for (int j = 1; j < n; j ++)
                                if (!M[j])
                                        T.push_back(PE[j]);
                        if (!R.size())
                        {
                                int mn = n, mx = -1;
                                vector < int > Val(n, 0);
                                for (int j = 0; j < (int)V.size(); j ++)
                                {
                                        vector < int > TMp = T;
                                        for (int h = 0; h < (int)V.size(); h ++)
                                                if (j != h)
                                                        TMp.push_back(V[h]);
                                        assert((int)TMp.size() == n - 1);
                                        Val[j] = count_common_roads(TMp);
                                        mn = min(mn, Val[j]);
                                        mx = max(mx, Val[j]);
                                }
                                if (mn == mx)
                                        for (int j : V)
                                                Stat[j] = 0;
                                else
                                {
                                        for (int j = 0; j < (int)V.size(); j ++)
                                                if (Val[j] == mn)
                                                        Stat[V[j]] = 1;
                                                else
                                                        Stat[V[j]] = 0;
                                }
                        }
                        else
                        {
                                vector < int > TMp = T;
                                for (int j : R)
                                        if (j != R[0])
                                                TMp.push_back(j);
                                for (int j : V)
                                        TMp.push_back(j);
                                assert(TMp.size() == n - 1);
                                int val = count_common_roads(TMp);

                                for (int j = 0; j < (int)V.size(); j ++)
                                {
                                        TMp = T;
                                        for (int h = 0; h < (int)V.size(); h ++)
                                                if (j != h)
                                                        TMp.push_back(V[h]);
                                        for (int h : R)
                                                TMp.push_back(h);
                                        assert((int)TMp.size() == n - 1);
                                        int cnt = count_common_roads(TMp);
                                        if (cnt < val)
                                                Stat[V[j]] = 1;
                                        else if (cnt > val)
                                                Stat[V[j]] = 0;
                                        else
                                                Stat[V[j]] = Stat[R[0]];
                                }
                        }
                }

        for (int i = 1; i < n; i ++)
                assert(Stat[PE[i]] != -1);

        for (int i = 1; i <= n; i ++)
        {
                vector < int > vec;
                for (auto u : Adj[i])
                        if (Stat[u.second] == -1)
                                vec.push_back(u.second);

                auto Ask = [&](int l, int r){
                        vector < int > TMp;
                        memset(F, -1, sizeof(F));
                        for (int j = l; j < r; j ++)
                        {
                                TMp.push_back(vec[j]);
                                Merge(A[vec[j]], B[vec[j]]);
                        }
                        int c = 0;
                        for (int i = 1; i < n; i ++)
                                if (Merge(i, P[i]))
                                        TMp.push_back(PE[i]), c -= Stat[PE[i]];
                        c += count_common_roads(TMp);
                        return c;
                };

                int d = Ask(0, (int)vec.size());

                int le = -1, ri = (int)vec.size(), md;
                for (int __ = 0; __ < d; __ ++)
                {
                        le ++;
                        ri = (int)vec.size();
                        while (ri - le > 1)
                        {
                                md = (le + ri) >> 1;
                                if (Ask(le, md))
                                        ri = md;
                                else
                                        le = md;
                        }
                        assert(Ask(le, le + 1));
                        Stat[vec[le]] = 1;
                }
                for (int id : vec)
                        if (Stat[id] == -1)
                                Stat[id] = 0;
        }

        vector < int > Rs;
        for (int i = 0; i < m; i ++)
                if (Stat[i])
                        Rs.push_back(i);
        return Rs;
}

Compilation message

In file included from /usr/include/c++/9/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:33,
                 from simurgh.cpp:2:
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:134:51: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  134 |                                 assert(TMp.size() == n - 1);
      |                                        ~~~~~~~~~~~^~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1280 KB correct
2 Correct 2 ms 1280 KB correct
3 Correct 1 ms 1280 KB correct
4 Correct 2 ms 1280 KB correct
5 Correct 1 ms 1280 KB correct
6 Correct 1 ms 1280 KB correct
7 Correct 1 ms 1280 KB correct
8 Correct 1 ms 1280 KB correct
9 Correct 1 ms 1280 KB correct
10 Correct 1 ms 1280 KB correct
11 Correct 1 ms 1280 KB correct
12 Correct 1 ms 1408 KB correct
13 Correct 1 ms 1408 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1280 KB correct
2 Correct 2 ms 1280 KB correct
3 Correct 1 ms 1280 KB correct
4 Correct 2 ms 1280 KB correct
5 Correct 1 ms 1280 KB correct
6 Correct 1 ms 1280 KB correct
7 Correct 1 ms 1280 KB correct
8 Correct 1 ms 1280 KB correct
9 Correct 1 ms 1280 KB correct
10 Correct 1 ms 1280 KB correct
11 Correct 1 ms 1280 KB correct
12 Correct 1 ms 1408 KB correct
13 Correct 1 ms 1408 KB correct
14 Correct 3 ms 1408 KB correct
15 Correct 3 ms 1408 KB correct
16 Correct 3 ms 1408 KB correct
17 Correct 3 ms 1408 KB correct
18 Correct 2 ms 1408 KB correct
19 Correct 3 ms 1408 KB correct
20 Correct 5 ms 1408 KB correct
21 Correct 3 ms 1408 KB correct
22 Correct 2 ms 1408 KB correct
23 Correct 2 ms 1408 KB correct
24 Correct 2 ms 1408 KB correct
25 Correct 2 ms 1408 KB correct
26 Correct 2 ms 1408 KB correct
27 Correct 2 ms 1408 KB correct
28 Correct 2 ms 1408 KB correct
29 Correct 2 ms 1408 KB correct
30 Correct 2 ms 1408 KB correct
31 Correct 2 ms 1408 KB correct
32 Correct 2 ms 1408 KB correct
33 Correct 2 ms 1408 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1280 KB correct
2 Correct 2 ms 1280 KB correct
3 Correct 1 ms 1280 KB correct
4 Correct 2 ms 1280 KB correct
5 Correct 1 ms 1280 KB correct
6 Correct 1 ms 1280 KB correct
7 Correct 1 ms 1280 KB correct
8 Correct 1 ms 1280 KB correct
9 Correct 1 ms 1280 KB correct
10 Correct 1 ms 1280 KB correct
11 Correct 1 ms 1280 KB correct
12 Correct 1 ms 1408 KB correct
13 Correct 1 ms 1408 KB correct
14 Correct 3 ms 1408 KB correct
15 Correct 3 ms 1408 KB correct
16 Correct 3 ms 1408 KB correct
17 Correct 3 ms 1408 KB correct
18 Correct 2 ms 1408 KB correct
19 Correct 3 ms 1408 KB correct
20 Correct 5 ms 1408 KB correct
21 Correct 3 ms 1408 KB correct
22 Correct 2 ms 1408 KB correct
23 Correct 2 ms 1408 KB correct
24 Correct 2 ms 1408 KB correct
25 Correct 2 ms 1408 KB correct
26 Correct 2 ms 1408 KB correct
27 Correct 2 ms 1408 KB correct
28 Correct 2 ms 1408 KB correct
29 Correct 2 ms 1408 KB correct
30 Correct 2 ms 1408 KB correct
31 Correct 2 ms 1408 KB correct
32 Correct 2 ms 1408 KB correct
33 Correct 2 ms 1408 KB correct
34 Correct 45 ms 2688 KB correct
35 Correct 46 ms 2688 KB correct
36 Correct 45 ms 2432 KB correct
37 Correct 12 ms 1408 KB correct
38 Correct 48 ms 2816 KB correct
39 Correct 46 ms 2560 KB correct
40 Correct 37 ms 2432 KB correct
41 Correct 50 ms 2688 KB correct
42 Correct 44 ms 2688 KB correct
43 Correct 20 ms 2304 KB correct
44 Correct 18 ms 1920 KB correct
45 Correct 21 ms 1984 KB correct
46 Correct 19 ms 1920 KB correct
47 Correct 11 ms 1664 KB correct
48 Correct 6 ms 1408 KB correct
49 Correct 7 ms 1408 KB correct
50 Correct 12 ms 1664 KB correct
51 Correct 19 ms 2048 KB correct
52 Correct 17 ms 1920 KB correct
53 Correct 20 ms 1920 KB correct
54 Correct 21 ms 2304 KB correct
55 Correct 20 ms 2048 KB correct
56 Correct 23 ms 2048 KB correct
57 Correct 21 ms 2048 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1280 KB correct
2 Correct 1 ms 1408 KB correct
3 Correct 153 ms 5736 KB correct
4 Correct 252 ms 7544 KB correct
5 Correct 277 ms 7416 KB correct
6 Correct 270 ms 7416 KB correct
7 Correct 240 ms 7416 KB correct
8 Correct 232 ms 7472 KB correct
9 Correct 247 ms 7416 KB correct
10 Correct 247 ms 7416 KB correct
11 Correct 247 ms 7544 KB correct
12 Correct 251 ms 7416 KB correct
13 Correct 1 ms 1280 KB correct
14 Correct 238 ms 7476 KB correct
15 Correct 247 ms 7416 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1280 KB correct
2 Correct 2 ms 1280 KB correct
3 Correct 1 ms 1280 KB correct
4 Correct 2 ms 1280 KB correct
5 Correct 1 ms 1280 KB correct
6 Correct 1 ms 1280 KB correct
7 Correct 1 ms 1280 KB correct
8 Correct 1 ms 1280 KB correct
9 Correct 1 ms 1280 KB correct
10 Correct 1 ms 1280 KB correct
11 Correct 1 ms 1280 KB correct
12 Correct 1 ms 1408 KB correct
13 Correct 1 ms 1408 KB correct
14 Correct 3 ms 1408 KB correct
15 Correct 3 ms 1408 KB correct
16 Correct 3 ms 1408 KB correct
17 Correct 3 ms 1408 KB correct
18 Correct 2 ms 1408 KB correct
19 Correct 3 ms 1408 KB correct
20 Correct 5 ms 1408 KB correct
21 Correct 3 ms 1408 KB correct
22 Correct 2 ms 1408 KB correct
23 Correct 2 ms 1408 KB correct
24 Correct 2 ms 1408 KB correct
25 Correct 2 ms 1408 KB correct
26 Correct 2 ms 1408 KB correct
27 Correct 2 ms 1408 KB correct
28 Correct 2 ms 1408 KB correct
29 Correct 2 ms 1408 KB correct
30 Correct 2 ms 1408 KB correct
31 Correct 2 ms 1408 KB correct
32 Correct 2 ms 1408 KB correct
33 Correct 2 ms 1408 KB correct
34 Correct 45 ms 2688 KB correct
35 Correct 46 ms 2688 KB correct
36 Correct 45 ms 2432 KB correct
37 Correct 12 ms 1408 KB correct
38 Correct 48 ms 2816 KB correct
39 Correct 46 ms 2560 KB correct
40 Correct 37 ms 2432 KB correct
41 Correct 50 ms 2688 KB correct
42 Correct 44 ms 2688 KB correct
43 Correct 20 ms 2304 KB correct
44 Correct 18 ms 1920 KB correct
45 Correct 21 ms 1984 KB correct
46 Correct 19 ms 1920 KB correct
47 Correct 11 ms 1664 KB correct
48 Correct 6 ms 1408 KB correct
49 Correct 7 ms 1408 KB correct
50 Correct 12 ms 1664 KB correct
51 Correct 19 ms 2048 KB correct
52 Correct 17 ms 1920 KB correct
53 Correct 20 ms 1920 KB correct
54 Correct 21 ms 2304 KB correct
55 Correct 20 ms 2048 KB correct
56 Correct 23 ms 2048 KB correct
57 Correct 21 ms 2048 KB correct
58 Correct 1 ms 1280 KB correct
59 Correct 1 ms 1408 KB correct
60 Correct 153 ms 5736 KB correct
61 Correct 252 ms 7544 KB correct
62 Correct 277 ms 7416 KB correct
63 Correct 270 ms 7416 KB correct
64 Correct 240 ms 7416 KB correct
65 Correct 232 ms 7472 KB correct
66 Correct 247 ms 7416 KB correct
67 Correct 247 ms 7416 KB correct
68 Correct 247 ms 7544 KB correct
69 Correct 251 ms 7416 KB correct
70 Correct 1 ms 1280 KB correct
71 Correct 238 ms 7476 KB correct
72 Correct 247 ms 7416 KB correct
73 Correct 1 ms 1280 KB correct
74 Correct 257 ms 7416 KB correct
75 Correct 251 ms 7288 KB correct
76 Correct 113 ms 3584 KB correct
77 Correct 260 ms 7416 KB correct
78 Correct 268 ms 7416 KB correct
79 Correct 252 ms 7424 KB correct
80 Correct 251 ms 7420 KB correct
81 Correct 219 ms 6776 KB correct
82 Correct 267 ms 7416 KB correct
83 Correct 170 ms 4728 KB correct
84 Correct 109 ms 5448 KB correct
85 Correct 100 ms 5240 KB correct
86 Correct 76 ms 3712 KB correct
87 Correct 63 ms 3328 KB correct
88 Correct 54 ms 2688 KB correct
89 Correct 57 ms 2680 KB correct
90 Correct 52 ms 2560 KB correct
91 Correct 22 ms 1536 KB correct
92 Correct 18 ms 1408 KB correct
93 Correct 101 ms 5240 KB correct
94 Correct 78 ms 3832 KB correct
95 Correct 76 ms 3712 KB correct
96 Correct 53 ms 2560 KB correct
97 Correct 56 ms 2688 KB correct
98 Correct 59 ms 3384 KB correct
99 Correct 60 ms 2688 KB correct
100 Correct 30 ms 1664 KB correct
101 Correct 16 ms 1408 KB correct
102 Correct 103 ms 4480 KB correct
103 Correct 103 ms 4480 KB correct
104 Correct 107 ms 4352 KB correct
105 Correct 101 ms 4344 KB correct