답안 #303375

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
303375 2020-09-20T09:15:50 Z Vimmer 슈퍼트리 잇기 (IOI20_supertrees) C++14
100 / 100
282 ms 26288 KB
#include <bits/stdc++.h>
#include "supertrees.h"
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

#define N 1005
#define PB push_back
#define sz(x) int(x.size())
#define P 31
#define F first
#define M ll(1e9 + 7)
#define S second
#define all(x) x.begin(), x.end()
#define endl '\n'

//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")

using namespace std;
//using namespace __gnu_pbds;

typedef long long ll;

//typedef tree<int, null_type, less_equal <int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

ll mlt(ll a, ll b) {return (a * b) % M;}
ll sm(ll a, ll b) {return (a + b) % M;}

int rk[N], pr[N], cnt[N][N], n, glob;

bool mk[N];

vector <int> g[N];

void make(int x) {pr[x] = x; rk[x] = 1;}
int fnd(int x) {if (pr[x] != x) pr[x] = fnd(pr[x]); return pr[x];}
void link(int a, int b)
{
    if (rk[b] > rk[a]) swap(a, b);

    rk[a] += rk[b];

    pr[b] = a;
}

//void build(vector <vector <int> > pr)
//{
//    for (int i = 0; i < sz(pr); i++)
//    {
//        cout << i << ": ";
//
//        for (auto itr : pr[i]) cout << itr << " "; cout << endl;
//    }
//}

void dfs(int v, int p)
{
    mk[v] = 1;

    cnt[glob][v]++;

    for (auto it : g[v])
      if (!mk[it]) dfs(it, v);

    mk[v] = 0;
}

int construct(vector<vector<int> > p)
{
	n = sz(p);

	vector<vector<int> > answer; answer.clear();

    bool f = 1, fr = 1, ft = 1;

    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            if (i != j) {if (p[i][j] == 2 || p[i][j] == 3) fr = 0; if (p[i][j] != 1) f = 0; if (p[i][j] == 1 || p[i][j] == 3) ft = 0;}

    if (f)
    {
        answer.resize(n);

        for (int i = 0; i < n; i++)
            answer[i].resize(n, 0);

        for (int i = 1; i < n; i++) answer[0][i] = 1;

        for (int i = 1; i < n; i++) answer[i][0] = 1;

        build(answer);

        return 1;
    }

    if (fr)
    {
        bool good = 1;

        answer.resize(n);

        for (int i = 0; i < n; i++)
            answer[i].resize(n, 0);

        for (int i = 0; i < n; i++) make(i);

        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                if (p[i][j] == 1)
                    {
                        int a = fnd(i), b = fnd(j);

                        if (a != b) {answer[i][j] = answer[j][i] = 1; link(a, b);}
                    }

        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
              if ((p[i][j] == 1 && fnd(i) != fnd(j)) || (p[i][j] == 0 && fnd(i) == fnd(j))) good = 0;

        if (good) build(answer);

        return good;
    }

    if (ft)
    {
        bool good = 1;

        answer.resize(n);

        for (int i = 0; i < n; i++)
            answer[i].resize(n, 0);

        for (int i = 0; i < n; i++) make(i);

        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                if (p[i][j] == 2)
                    {
                        int a = fnd(i), b = fnd(j);

                        if (a != b) link(a, b);
                    }

        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
              if (i != j && ((p[i][j] == 2 && fnd(i) != fnd(j)) || (p[i][j] == 0 && fnd(i) == fnd(j)))) good = 0;

        if (!good) return 0;

        set <int> se; se.clear();

        for (int i = 0; i < n; i++)
        {
            int id = fnd(i);

            if (se.find(id) != se.end()) continue;

            se.insert(id);

            vector <int> pr; pr.clear();

            for (int j = 0; j < n; j++)
                if (fnd(j) == id)
                  pr.PB(j);

            if (sz(pr) == 1) continue;

            if (sz(pr) == 2) {good = 0; break;}

            for (int j = 0; j < sz(pr); j++)
            {
                int nxt = (j + 1) % sz(pr);

                answer[pr[j]][pr[nxt]] = answer[pr[nxt]][pr[j]] = 1;
            }
        }

        if (!good) return 0;

        build(answer);

        return 1;
    }

    bool good = 1;

    answer.resize(n);

    for (int i = 0; i < n; i++)
        answer[i].resize(n, 0);

    for (int i = 0; i < n; i++) make(i);

    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            if (p[i][j] == 1 || p[i][j] == 2)
                {
                    int a = fnd(i), b = fnd(j);

                    if (a != b) link(a, b);
                }

    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
          if ((p[i][j] == 1 && fnd(i) != fnd(j)) || (p[i][j] == 0 && fnd(i) == fnd(j))) good = 0;

    if (!good) return 0;

    bool mk[n], mkr[n];

    for (int i = 0; i < n; i++)
    {
        mk[i] = 1;

        mkr[i] = 1;

        for (int j = 0; j < n; j++)
            if (i != j && p[i][j] == 1) mk[i] = 0;

        for (int j = 0; j < n; j++)
            if (i != j && p[i][j] == 2) mkr[i] = 0;
    }

    set <int> se, str; se.clear(); str.clear();

    for (int i = 0; i < n; i++)
    {
        int id = fnd(i);

        if (se.find(id) != se.end()) continue;

        se.insert(id);

        vector <int> c; c.clear();

        vector <int> pr; pr.clear();

        bool bd = 1;

        for (int j = 0; j < n; j++)
        {
            if (fnd(j) != fnd(i)) continue;

            if (mk[j]) c.PB(j);
              else pr.PB(j);

            if (!mkr[j]) bd = 0;
        }

        if (sz(c) == 0)
        {
            if (bd)
            {
                for (int j = 1; j < sz(pr); j++)
                {
                    int nxt = pr[j - 1];

                    answer[pr[j]][nxt] = answer[nxt][pr[j]] = 1;
                }

                continue;
            }

            int l = -1, r = -1;

            for (int j = 0; j < sz(pr); j++)
            {
                auto it = pr[j];

                if (str.find(it) != str.end()) continue;

                str.insert(it);

                if (r != -1) answer[r][it] = answer[it][r] = 1;
                  else l = it;

                r = it;

                int lst = it;

                for (int u = j + 1; u < sz(pr); u++)
                    if (p[pr[u]][it] == 1)
                    {
                        str.insert(pr[u]);

                        answer[pr[u]][lst] = answer[lst][pr[u]] = 1;

                        lst = pr[u];
                    }
            }

            answer[l][r] = answer[r][l] = 1;

            continue;
        }

        for (int j = 1; j < sz(c); j++) answer[c[j]][c[j - 1]] = answer[c[j - 1]][c[j]] = 1;

        int l = c[0], r = c.back();

        for (int j = 0; j < sz(pr); j++)
        {
            auto it = pr[j];

            if (str.find(it) != str.end()) continue;

            str.insert(it);

            answer[l][it] = answer[it][l] = 1;

            l = it;

            int lst = it;

            for (int u = j + 1; u < sz(pr); u++)
                if (p[pr[u]][it] == 1)
                {
                    str.insert(pr[u]);

                    answer[pr[u]][lst] = answer[lst][pr[u]] = 1;

                    lst = pr[u];
                }
        }

        answer[l][r] = answer[r][l] = 1;
    }

    for (int i = 0; i < n; i++) answer[i][i] = 0;

    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++) if (answer[i][j] == 1) g[i].PB(j);

    for (int i = 0; i < n; i++)
    {
        glob = i;

        dfs(i, -1);

        for (int j = 0; j < n; j++)
            if (cnt[i][j] != p[i][j]) return 0;
    }

    build(answer);

    return 1;
}

//int main()
//{
////    freopen("help.in", "r", stdin); freopen("help.out", "w", stdout);
//
//    ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//
//    vector <vector <int> > vr(6);
//
//    for (int i = 0; i < 6; i++) {vr[i].resize(6, 0); vr[i][i] = 1;}
//
//    vr[0][3] = vr[3][0] = vr[2][4] = vr[4][2] = vr[1][5] = vr[5][1] = 1;
//
//    for (int i = 0; i < 6; i++)
//        for (int j = 0; j < 6; j++)
//          if (vr[i][j] != 1) vr[i][j] = 2;
//
//    cout << construct(vr) << endl;
//}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 11 ms 1280 KB Output is correct
7 Correct 250 ms 22256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 11 ms 1280 KB Output is correct
7 Correct 250 ms 22256 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 10 ms 1280 KB Output is correct
13 Correct 246 ms 22264 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 7 ms 768 KB Output is correct
17 Correct 132 ms 12152 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 62 ms 5880 KB Output is correct
21 Correct 251 ms 22136 KB Output is correct
22 Correct 245 ms 22140 KB Output is correct
23 Correct 277 ms 22136 KB Output is correct
24 Correct 245 ms 22136 KB Output is correct
25 Correct 110 ms 12156 KB Output is correct
26 Correct 115 ms 12152 KB Output is correct
27 Correct 274 ms 22136 KB Output is correct
28 Correct 255 ms 22136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 10 ms 1280 KB Output is correct
9 Correct 255 ms 22136 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 11 ms 1280 KB Output is correct
13 Correct 271 ms 22136 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 6 ms 768 KB Output is correct
17 Correct 146 ms 12152 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 62 ms 5880 KB Output is correct
22 Correct 258 ms 22136 KB Output is correct
23 Correct 248 ms 22256 KB Output is correct
24 Correct 282 ms 22140 KB Output is correct
25 Correct 107 ms 12152 KB Output is correct
26 Correct 120 ms 12152 KB Output is correct
27 Correct 248 ms 22136 KB Output is correct
28 Correct 282 ms 22136 KB Output is correct
29 Correct 108 ms 12152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 65 ms 5880 KB Output is correct
5 Correct 252 ms 22136 KB Output is correct
6 Correct 248 ms 22136 KB Output is correct
7 Correct 268 ms 22136 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 63 ms 5880 KB Output is correct
10 Correct 251 ms 22136 KB Output is correct
11 Correct 244 ms 22136 KB Output is correct
12 Correct 272 ms 22264 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 65 ms 7856 KB Output is correct
17 Correct 251 ms 22136 KB Output is correct
18 Correct 268 ms 26288 KB Output is correct
19 Correct 255 ms 26104 KB Output is correct
20 Correct 258 ms 26104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 11 ms 1280 KB Output is correct
7 Correct 250 ms 22256 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 10 ms 1280 KB Output is correct
13 Correct 246 ms 22264 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 7 ms 768 KB Output is correct
17 Correct 132 ms 12152 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 62 ms 5880 KB Output is correct
21 Correct 251 ms 22136 KB Output is correct
22 Correct 245 ms 22140 KB Output is correct
23 Correct 277 ms 22136 KB Output is correct
24 Correct 245 ms 22136 KB Output is correct
25 Correct 110 ms 12156 KB Output is correct
26 Correct 115 ms 12152 KB Output is correct
27 Correct 274 ms 22136 KB Output is correct
28 Correct 255 ms 22136 KB Output is correct
29 Correct 0 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
31 Correct 1 ms 512 KB Output is correct
32 Correct 0 ms 384 KB Output is correct
33 Correct 0 ms 384 KB Output is correct
34 Correct 1 ms 384 KB Output is correct
35 Correct 1 ms 384 KB Output is correct
36 Correct 10 ms 1280 KB Output is correct
37 Correct 255 ms 22136 KB Output is correct
38 Correct 1 ms 384 KB Output is correct
39 Correct 0 ms 384 KB Output is correct
40 Correct 11 ms 1280 KB Output is correct
41 Correct 271 ms 22136 KB Output is correct
42 Correct 1 ms 384 KB Output is correct
43 Correct 1 ms 384 KB Output is correct
44 Correct 6 ms 768 KB Output is correct
45 Correct 146 ms 12152 KB Output is correct
46 Correct 1 ms 384 KB Output is correct
47 Correct 1 ms 384 KB Output is correct
48 Correct 1 ms 384 KB Output is correct
49 Correct 62 ms 5880 KB Output is correct
50 Correct 258 ms 22136 KB Output is correct
51 Correct 248 ms 22256 KB Output is correct
52 Correct 282 ms 22140 KB Output is correct
53 Correct 107 ms 12152 KB Output is correct
54 Correct 120 ms 12152 KB Output is correct
55 Correct 248 ms 22136 KB Output is correct
56 Correct 282 ms 22136 KB Output is correct
57 Correct 108 ms 12152 KB Output is correct
58 Correct 0 ms 384 KB Output is correct
59 Correct 0 ms 384 KB Output is correct
60 Correct 6 ms 768 KB Output is correct
61 Correct 138 ms 12152 KB Output is correct
62 Correct 1 ms 384 KB Output is correct
63 Correct 0 ms 384 KB Output is correct
64 Correct 1 ms 384 KB Output is correct
65 Correct 65 ms 7800 KB Output is correct
66 Correct 116 ms 12152 KB Output is correct
67 Correct 126 ms 12152 KB Output is correct
68 Correct 110 ms 12152 KB Output is correct
69 Correct 114 ms 12280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 11 ms 1280 KB Output is correct
7 Correct 250 ms 22256 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 10 ms 1280 KB Output is correct
13 Correct 246 ms 22264 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 7 ms 768 KB Output is correct
17 Correct 132 ms 12152 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 62 ms 5880 KB Output is correct
21 Correct 251 ms 22136 KB Output is correct
22 Correct 245 ms 22140 KB Output is correct
23 Correct 277 ms 22136 KB Output is correct
24 Correct 245 ms 22136 KB Output is correct
25 Correct 110 ms 12156 KB Output is correct
26 Correct 115 ms 12152 KB Output is correct
27 Correct 274 ms 22136 KB Output is correct
28 Correct 255 ms 22136 KB Output is correct
29 Correct 0 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
31 Correct 1 ms 512 KB Output is correct
32 Correct 0 ms 384 KB Output is correct
33 Correct 0 ms 384 KB Output is correct
34 Correct 1 ms 384 KB Output is correct
35 Correct 1 ms 384 KB Output is correct
36 Correct 10 ms 1280 KB Output is correct
37 Correct 255 ms 22136 KB Output is correct
38 Correct 1 ms 384 KB Output is correct
39 Correct 0 ms 384 KB Output is correct
40 Correct 11 ms 1280 KB Output is correct
41 Correct 271 ms 22136 KB Output is correct
42 Correct 1 ms 384 KB Output is correct
43 Correct 1 ms 384 KB Output is correct
44 Correct 6 ms 768 KB Output is correct
45 Correct 146 ms 12152 KB Output is correct
46 Correct 1 ms 384 KB Output is correct
47 Correct 1 ms 384 KB Output is correct
48 Correct 1 ms 384 KB Output is correct
49 Correct 62 ms 5880 KB Output is correct
50 Correct 258 ms 22136 KB Output is correct
51 Correct 248 ms 22256 KB Output is correct
52 Correct 282 ms 22140 KB Output is correct
53 Correct 107 ms 12152 KB Output is correct
54 Correct 120 ms 12152 KB Output is correct
55 Correct 248 ms 22136 KB Output is correct
56 Correct 282 ms 22136 KB Output is correct
57 Correct 108 ms 12152 KB Output is correct
58 Correct 0 ms 384 KB Output is correct
59 Correct 0 ms 384 KB Output is correct
60 Correct 0 ms 384 KB Output is correct
61 Correct 65 ms 5880 KB Output is correct
62 Correct 252 ms 22136 KB Output is correct
63 Correct 248 ms 22136 KB Output is correct
64 Correct 268 ms 22136 KB Output is correct
65 Correct 0 ms 384 KB Output is correct
66 Correct 63 ms 5880 KB Output is correct
67 Correct 251 ms 22136 KB Output is correct
68 Correct 244 ms 22136 KB Output is correct
69 Correct 272 ms 22264 KB Output is correct
70 Correct 1 ms 384 KB Output is correct
71 Correct 1 ms 384 KB Output is correct
72 Correct 1 ms 384 KB Output is correct
73 Correct 65 ms 7856 KB Output is correct
74 Correct 251 ms 22136 KB Output is correct
75 Correct 268 ms 26288 KB Output is correct
76 Correct 255 ms 26104 KB Output is correct
77 Correct 258 ms 26104 KB Output is correct
78 Correct 0 ms 384 KB Output is correct
79 Correct 0 ms 384 KB Output is correct
80 Correct 6 ms 768 KB Output is correct
81 Correct 138 ms 12152 KB Output is correct
82 Correct 1 ms 384 KB Output is correct
83 Correct 0 ms 384 KB Output is correct
84 Correct 1 ms 384 KB Output is correct
85 Correct 65 ms 7800 KB Output is correct
86 Correct 116 ms 12152 KB Output is correct
87 Correct 126 ms 12152 KB Output is correct
88 Correct 110 ms 12152 KB Output is correct
89 Correct 114 ms 12280 KB Output is correct
90 Correct 0 ms 384 KB Output is correct
91 Correct 1 ms 384 KB Output is correct
92 Correct 6 ms 768 KB Output is correct
93 Correct 138 ms 12152 KB Output is correct
94 Correct 1 ms 384 KB Output is correct
95 Correct 1 ms 384 KB Output is correct
96 Correct 1 ms 384 KB Output is correct
97 Correct 35 ms 3584 KB Output is correct
98 Correct 137 ms 12400 KB Output is correct
99 Correct 134 ms 12792 KB Output is correct
100 Correct 115 ms 12536 KB Output is correct
101 Correct 116 ms 13560 KB Output is correct
102 Correct 125 ms 12280 KB Output is correct
103 Correct 144 ms 12276 KB Output is correct
104 Correct 147 ms 12280 KB Output is correct
105 Correct 134 ms 12280 KB Output is correct