답안 #503202

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
503202 2022-01-07T13:33:04 Z LouayFarah 슈퍼트리 잇기 (IOI20_supertrees) C++14
40 / 100
179 ms 22284 KB
#include "bits/stdc++.h"

using namespace std;

#define endl "\n"
#define ll long long int
#define pb push_back
#define mp make_pair
#define fi first
#define se second

const long long MOD = 1e9+7;
const long long INF = 1e18;

int nx[4] = {0, 0, -1, 1};
int ny[4] = {1, -1, 0, 0};

void build(vector<vector<int>> b);

struct dsu
{
    vector<int> len;
    vector<int> link;

    void init(int n)
    {
        len.assign(n, 1);
        link.assign(n, 0);
        for(int i = 0; i<n; i++)
        {
            link[i] = i;
        }
    }

    int search(int x)
    {
        if(x!=link[x])
            link[x] = search(link[x]);
        return link[x];
    }

    bool check(int a, int b)
    {
        return search(a)==search(b);
    }

    void join(int a, int b)
    {
        a = search(a);
        b = search(b);

        if(a!=b)
        {
            if(len[a]<len[b])
                swap(a, b);

            len[a]+=len[b];
            link[b] = a;
        }
    }
};

int construct(vector<vector<int>> p)
{
    int n = int(p[0].size());

    vector<vector<int>> b(n, vector<int>(n, 0));

    dsu d;
    d.init(n);

    bool flag1 = false;
    bool flag2 = true;

    for(int i = 0; i<n; i++)
    {
        for(int j = i+1; j<n; j++)
        {
            if(p[i][j]==1)
            {
                d.join(i, j);
                flag1 = true;
            }
            if(p[i][j]==2)
            {
                d.join(i, j);
                flag2 = true;
            }
        }
    }

    vector<vector<int>> groups(n);
    for(int i = 0; i<n; i++)
    {
        int parent = d.search(i);
        groups[parent].pb(i);
    }

    /*for(int i = 0; i<n; i++)
    {
        cout << i << ": ";
        for(auto elt: groups[i])
            cout << elt << ' ';
        cout << endl;
    }*/


    if(!flag2&&flag1)
    {
        for(int i = 0; i<n; i++)
        {
            if(groups[i].empty())
                continue;

            int len = int(groups[i].size());
            for(int j = 0; j<len-1; j++)
            {

                int A = groups[i][j];
                int B = groups[i][j+1];

                b[A][B] = 1;
                b[B][A] = 1;
            }
        }
    }
    else if(!flag1&&flag2)
    {
        for(int i = 0; i<n; i++)
        {
            if(groups[i].empty())
                continue;

            if(int(groups[i].size())==2)
                return 0;

            int len = int(groups[i].size());
            for(int j = 0; j<len-1; j++)
            {

                int A = groups[i][j];
                int B = groups[i][j+1];

                b[A][B] = 1;
                b[B][A] = 1;
            }
            int A = groups[i][len-1];
            int B = groups[i][0];

            b[A][B] = 1;
            b[B][A] = 1;
        }
    }
    else
    {
        for(auto group: groups)
        {
            if(group.empty())
                continue;


            vector<int> one, two;
            int len = int(group.size());
            for(int i = 0; i<len; i++)
            {
                int A = group[i];
                bool only2 = true;
                for(int j = 0; j<len; j++)
                {
                    if(i==j)
                        continue;
                    int B = group[j];
                    if(p[A][B]==1)
                    {
                        only2 = false;
                        break;
                    }
                }

                if(only2)
                    two.pb(A);
                else
                    one.pb(A);
            }

            if(two.empty())
            {
                for(int j = 0; j<int(one.size())-1; j++)
                {

                    int A = one[j];
                    int B = one[j+1];

                    b[A][B] = 1;
                    b[B][A] = 1;
                }
            }
            else if(one.empty())
            {
                if(int(two.size())==2)
                    return 0;
                for(int j = 0; j<int(two.size())-1; j++)
                {
                    int A = two[j];
                    int B = two[j+1];

                    b[A][B] = 1;
                    b[B][A] = 1;
                }
                int A = two[int(two.size())-1];
                int B = two[0];

                b[A][B] = 1;
                b[B][A] = 1;
            }
            else
            {
                for(int j = 0; j<int(one.size())-1; j++)
                {

                    int A = one[j];
                    int B = one[j+1];

                    b[A][B] = 1;
                    b[B][A] = 1;
                }

                for(int j = 0; j<int(two.size())-1; j++)
                {
                    int A = two[j];
                    int B = two[j+1];

                    b[A][B] = 1;
                    b[B][A] = 1;
                }

                int A = one[0];
                int B = two[0];
                int C = two[int(two.size())-1];

                b[A][B] = 1;
                b[B][A] = 1;
                b[A][C] = 1;
                b[C][A] = 1;
            }
        }
    }

    for(int i = 0; i<n; i++)
    {
        for(int j = i+1; j<n; j++)
        {
            if(p[i][j]==0&&d.check(i, j))
                return 0;
            if(!d.check(i, j)&&p[i][j]!=0)
                return 0;
        }
    }

    for(int i = 0; i<n; i++)
    {
        b[i][i]  = 0;
    }

    build(b);
    return 1;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 8 ms 1136 KB Output is correct
7 Correct 163 ms 22132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 8 ms 1136 KB Output is correct
7 Correct 163 ms 22132 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 8 ms 1180 KB Output is correct
13 Correct 163 ms 22020 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 3 ms 716 KB Output is correct
17 Correct 77 ms 12156 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 42 ms 5688 KB Output is correct
21 Correct 171 ms 22012 KB Output is correct
22 Correct 169 ms 22140 KB Output is correct
23 Correct 177 ms 22008 KB Output is correct
24 Correct 168 ms 22024 KB Output is correct
25 Correct 70 ms 12156 KB Output is correct
26 Correct 70 ms 12156 KB Output is correct
27 Correct 177 ms 22008 KB Output is correct
28 Correct 175 ms 22284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 7 ms 1100 KB Output is correct
9 Correct 170 ms 22036 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 7 ms 1100 KB Output is correct
13 Correct 166 ms 22020 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 4 ms 716 KB Output is correct
17 Correct 77 ms 12168 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 43 ms 5752 KB Output is correct
22 Correct 166 ms 22008 KB Output is correct
23 Correct 166 ms 22024 KB Output is correct
24 Correct 175 ms 22084 KB Output is correct
25 Correct 70 ms 12100 KB Output is correct
26 Correct 74 ms 12228 KB Output is correct
27 Correct 167 ms 22012 KB Output is correct
28 Correct 174 ms 22024 KB Output is correct
29 Correct 68 ms 12168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 43 ms 5668 KB Output is correct
5 Correct 169 ms 21972 KB Output is correct
6 Correct 166 ms 22072 KB Output is correct
7 Correct 179 ms 22008 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 46 ms 5692 KB Output is correct
10 Correct 169 ms 22144 KB Output is correct
11 Correct 167 ms 22008 KB Output is correct
12 Correct 174 ms 22008 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Incorrect 43 ms 5744 KB Too few ways to get from 0 to 24, should be 2 found 1
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 8 ms 1136 KB Output is correct
7 Correct 163 ms 22132 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 8 ms 1180 KB Output is correct
13 Correct 163 ms 22020 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 3 ms 716 KB Output is correct
17 Correct 77 ms 12156 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 42 ms 5688 KB Output is correct
21 Correct 171 ms 22012 KB Output is correct
22 Correct 169 ms 22140 KB Output is correct
23 Correct 177 ms 22008 KB Output is correct
24 Correct 168 ms 22024 KB Output is correct
25 Correct 70 ms 12156 KB Output is correct
26 Correct 70 ms 12156 KB Output is correct
27 Correct 177 ms 22008 KB Output is correct
28 Correct 175 ms 22284 KB Output is correct
29 Correct 0 ms 208 KB Output is correct
30 Correct 0 ms 204 KB Output is correct
31 Correct 0 ms 204 KB Output is correct
32 Correct 0 ms 204 KB Output is correct
33 Correct 0 ms 204 KB Output is correct
34 Correct 0 ms 204 KB Output is correct
35 Correct 0 ms 204 KB Output is correct
36 Correct 7 ms 1100 KB Output is correct
37 Correct 170 ms 22036 KB Output is correct
38 Correct 0 ms 204 KB Output is correct
39 Correct 0 ms 204 KB Output is correct
40 Correct 7 ms 1100 KB Output is correct
41 Correct 166 ms 22020 KB Output is correct
42 Correct 0 ms 204 KB Output is correct
43 Correct 0 ms 204 KB Output is correct
44 Correct 4 ms 716 KB Output is correct
45 Correct 77 ms 12168 KB Output is correct
46 Correct 0 ms 204 KB Output is correct
47 Correct 0 ms 204 KB Output is correct
48 Correct 0 ms 204 KB Output is correct
49 Correct 43 ms 5752 KB Output is correct
50 Correct 166 ms 22008 KB Output is correct
51 Correct 166 ms 22024 KB Output is correct
52 Correct 175 ms 22084 KB Output is correct
53 Correct 70 ms 12100 KB Output is correct
54 Correct 74 ms 12228 KB Output is correct
55 Correct 167 ms 22012 KB Output is correct
56 Correct 174 ms 22024 KB Output is correct
57 Correct 68 ms 12168 KB Output is correct
58 Correct 0 ms 204 KB Output is correct
59 Correct 0 ms 204 KB Output is correct
60 Correct 4 ms 716 KB Output is correct
61 Correct 77 ms 12100 KB Output is correct
62 Correct 0 ms 204 KB Output is correct
63 Correct 0 ms 204 KB Output is correct
64 Correct 1 ms 204 KB Output is correct
65 Incorrect 44 ms 5760 KB Too few ways to get from 1 to 11, should be 2 found 1
66 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 8 ms 1136 KB Output is correct
7 Correct 163 ms 22132 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 8 ms 1180 KB Output is correct
13 Correct 163 ms 22020 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 3 ms 716 KB Output is correct
17 Correct 77 ms 12156 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 42 ms 5688 KB Output is correct
21 Correct 171 ms 22012 KB Output is correct
22 Correct 169 ms 22140 KB Output is correct
23 Correct 177 ms 22008 KB Output is correct
24 Correct 168 ms 22024 KB Output is correct
25 Correct 70 ms 12156 KB Output is correct
26 Correct 70 ms 12156 KB Output is correct
27 Correct 177 ms 22008 KB Output is correct
28 Correct 175 ms 22284 KB Output is correct
29 Correct 0 ms 208 KB Output is correct
30 Correct 0 ms 204 KB Output is correct
31 Correct 0 ms 204 KB Output is correct
32 Correct 0 ms 204 KB Output is correct
33 Correct 0 ms 204 KB Output is correct
34 Correct 0 ms 204 KB Output is correct
35 Correct 0 ms 204 KB Output is correct
36 Correct 7 ms 1100 KB Output is correct
37 Correct 170 ms 22036 KB Output is correct
38 Correct 0 ms 204 KB Output is correct
39 Correct 0 ms 204 KB Output is correct
40 Correct 7 ms 1100 KB Output is correct
41 Correct 166 ms 22020 KB Output is correct
42 Correct 0 ms 204 KB Output is correct
43 Correct 0 ms 204 KB Output is correct
44 Correct 4 ms 716 KB Output is correct
45 Correct 77 ms 12168 KB Output is correct
46 Correct 0 ms 204 KB Output is correct
47 Correct 0 ms 204 KB Output is correct
48 Correct 0 ms 204 KB Output is correct
49 Correct 43 ms 5752 KB Output is correct
50 Correct 166 ms 22008 KB Output is correct
51 Correct 166 ms 22024 KB Output is correct
52 Correct 175 ms 22084 KB Output is correct
53 Correct 70 ms 12100 KB Output is correct
54 Correct 74 ms 12228 KB Output is correct
55 Correct 167 ms 22012 KB Output is correct
56 Correct 174 ms 22024 KB Output is correct
57 Correct 68 ms 12168 KB Output is correct
58 Correct 0 ms 204 KB Output is correct
59 Correct 0 ms 204 KB Output is correct
60 Correct 0 ms 204 KB Output is correct
61 Correct 43 ms 5668 KB Output is correct
62 Correct 169 ms 21972 KB Output is correct
63 Correct 166 ms 22072 KB Output is correct
64 Correct 179 ms 22008 KB Output is correct
65 Correct 0 ms 204 KB Output is correct
66 Correct 46 ms 5692 KB Output is correct
67 Correct 169 ms 22144 KB Output is correct
68 Correct 167 ms 22008 KB Output is correct
69 Correct 174 ms 22008 KB Output is correct
70 Correct 0 ms 204 KB Output is correct
71 Correct 0 ms 204 KB Output is correct
72 Correct 0 ms 204 KB Output is correct
73 Incorrect 43 ms 5744 KB Too few ways to get from 0 to 24, should be 2 found 1
74 Halted 0 ms 0 KB -