답안 #479809

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
479809 2021-10-13T08:08:04 Z stefantaga 슈퍼트리 잇기 (IOI20_supertrees) C++14
100 / 100
280 ms 32196 KB
#include "supertrees.h"
#include <vector>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>

using namespace std;
int viz[1005],q,componentus[1005],n;
vector <int> comp[1005];
vector <vector<int>> p;
void dfs(int x)
{
    viz[x]=1;
    comp[q].push_back(x);
    componentus[x]=q;
    for (int j=0;j<n;j++)
    {
        if (viz[j]==0&&p[j][x]!=0)
        {
            dfs(j);
        }
    }
}
int matrice[1005][1005],rang[1005],tata[1005],fr[1005];
int parinte (int x)
{
    if (tata[x]!=x)
    {
        return parinte(tata[x]);
    }
    return x;
}
void uneste(int x,int y)
{
    x=parinte(x);
    y=parinte(y);
    if (x==y)
    {
        return ;
    }
    if (rang[x]<rang[y])
    {
        tata[x]=y;
    }
    else
    if (rang[x]>rang[y])
    {
        tata[y]=x;
    }
    else
    {
        tata[x]=y;
        rang[y]++;
    }
}
void dfs2(int x)
{
    viz[x]=1;
    for (int j=0;j<n;j++)
    {
        if (viz[j]==0&&p[j][x]==1)
        {
            matrice[x][j]=matrice[j][x]=1;
            dfs2(j);
        }
    }
}
vector <int> comp2[1005];
int construct(std::vector<std::vector<int>> alo) {
	n = alo.size();
	int i,j,k;
	p=alo;
	q=0;
	for (i=0;i<n;i++)
    {
        for (j=0;j<n;j++)
        {
            if (p[i][j]==3)
            {
                return 0;
            }
        }
    }
    for (i=0;i<n;i++)
    {
        if (viz[i]==0)
        {
            q++;
            dfs(i);
        }
    }
    for (i=1;i<=q;i++)
    {
        for (j=0;j<comp[i].size();j++)
        {
            for (k=0;k<comp[i].size();k++)
            {
                int nod1=comp[i][j];
                int nod2=comp[i][k];
                if (p[nod1][nod2]==0)
                {
                    return 0;
                }
            }
        }
    }
    for (i=0;i<n;i++)
    {
        tata[i]=i;
        rang[i]=1;
    }
    for (i=0;i<n;i++)
    {
        for (j=0;j<n;j++)
        {
            if (p[i][j]==1)
            {
                uneste(i,j);
            }
        }
    }
    for (i=0;i<n;i++)
    {
        fr[parinte(i)]=1;
        comp2[parinte(i)].push_back(i);
    }
    for (i=0;i<n;i++)
    {
        if (fr[i]==1)
        {
            for (int j=0;j<comp2[i].size();j++)
            {
                for (int t=0;t<comp2[i].size();t++)
                {
                    int nod1=comp2[i][j];
                    int nod2=comp2[i][t];
                    if (p[nod1][nod2]!=1)
                    {
                        return 0;
                    }
                }
            }
        }
    }
    for (i=0;i<n;i++)
    {
        viz[i]=0;
    }
    for (i=1;i<=q;i++)
    {
        vector <int> ceau;
        for (j=0;j<comp[i].size();j++)
        {
            int nod1=comp[i][j];
            if (viz[nod1]==0)
            {
                ceau.push_back(nod1);
                dfs2(nod1);
            }
        }
        if (ceau.size()==2)
        {
            return 0;
        }
        if (ceau.size()==1)
        {
            continue;
        }
        for (j=0;j<ceau.size()-1;j++)
        {
            matrice[ceau[j]][ceau[j+1]]=matrice[ceau[j+1]][ceau[j]]=1;
        }
        matrice[ceau[0]][ceau[ceau.size()-1]]=matrice[ceau[ceau.size()-1]][ceau[0]]=1;
    }
    vector <vector<int>> sol;
    sol.resize(n);
    for (i=0;i<n;i++)
    {
        sol[i].resize(n);
    }
    for (i=0;i<n;i++)
    {
        for (j=0;j<n;j++)
        {
            sol[i][j]=matrice[i][j];
        }
    }
    build(sol);
	return 1;
}


#ifdef HOME
static int n2;
static std::vector<std::vector<int>> p3;
static std::vector<std::vector<int>> b;
static bool called = false;

static void check(bool cond, std::string message) {
    if (!cond) {
        printf("%s\n", message.c_str());
        fclose(stdout);
        exit(0);
    }
}

void build(std::vector<std::vector<int>> _b) {
    check(!called, "build is called more than once");
    called = true;
    check((int)_b.size() == n2, "Invalid number of rows in b");
    for (int i = 0; i < n2; i++) {
        check((int)_b[i].size() == n2, "Invalid number of columns in b");
    }
    b = _b;
}

int main() {
    assert(scanf("%d", &n2) == 1);

    p3.resize(n2);
    for (int i = 0; i < n2; i++) {
        p3[i].resize(n2);
    }

    for (int i = 0; i < n2; i++) {
        for (int j = 0; j < n2; j++) {
            assert(scanf("%d", &p3[i][j]) == 1);
        }
    }
    fclose(stdin);

    int possible = construct(p3);

    check(possible == 0 || possible == 1, "Invalid return value of construct");
    if (possible == 1) {
        check(called, "construct returned 1 without calling build");
    } else {
        check(!called, "construct called build but returned 0");
    }

    printf("%d\n", possible);
    if (possible == 1) {
        for (int i = 0; i < n2; i++) {
            for (int j = 0; j < n2; j++) {
                if (j) {
                    printf(" ");
                }
                printf("%d", b[i][j]);
            }
            printf("\n");
        }
    }
    fclose(stdout);
}

#endif // HOME

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:96:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |         for (j=0;j<comp[i].size();j++)
      |                  ~^~~~~~~~~~~~~~~
supertrees.cpp:98:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |             for (k=0;k<comp[i].size();k++)
      |                      ~^~~~~~~~~~~~~~~
supertrees.cpp:133:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  133 |             for (int j=0;j<comp2[i].size();j++)
      |                          ~^~~~~~~~~~~~~~~~
supertrees.cpp:135:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  135 |                 for (int t=0;t<comp2[i].size();t++)
      |                              ~^~~~~~~~~~~~~~~~
supertrees.cpp:154:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  154 |         for (j=0;j<comp[i].size();j++)
      |                  ~^~~~~~~~~~~~~~~
supertrees.cpp:171:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  171 |         for (j=0;j<ceau.size()-1;j++)
      |                  ~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 11 ms 2284 KB Output is correct
7 Correct 247 ms 31908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 11 ms 2284 KB Output is correct
7 Correct 247 ms 31908 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 0 ms 336 KB Output is correct
12 Correct 9 ms 1484 KB Output is correct
13 Correct 229 ms 28068 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 5 ms 972 KB Output is correct
17 Correct 112 ms 14104 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 73 ms 9096 KB Output is correct
21 Correct 251 ms 31940 KB Output is correct
22 Correct 236 ms 31564 KB Output is correct
23 Correct 260 ms 31900 KB Output is correct
24 Correct 233 ms 28192 KB Output is correct
25 Correct 106 ms 14020 KB Output is correct
26 Correct 104 ms 14120 KB Output is correct
27 Correct 240 ms 31940 KB Output is correct
28 Correct 230 ms 28200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 11 ms 1376 KB Output is correct
9 Correct 245 ms 28068 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 11 ms 2252 KB Output is correct
13 Correct 230 ms 31912 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 5 ms 844 KB Output is correct
17 Correct 110 ms 14108 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 68 ms 9060 KB Output is correct
22 Correct 253 ms 32072 KB Output is correct
23 Correct 228 ms 31940 KB Output is correct
24 Correct 241 ms 31948 KB Output is correct
25 Correct 109 ms 14360 KB Output is correct
26 Correct 112 ms 14024 KB Output is correct
27 Correct 252 ms 31816 KB Output is correct
28 Correct 248 ms 31956 KB Output is correct
29 Correct 117 ms 14404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 63 ms 9120 KB Output is correct
5 Correct 253 ms 31944 KB Output is correct
6 Correct 241 ms 31428 KB Output is correct
7 Correct 280 ms 31940 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 67 ms 9156 KB Output is correct
10 Correct 274 ms 31916 KB Output is correct
11 Correct 242 ms 31940 KB Output is correct
12 Correct 245 ms 32196 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 63 ms 9156 KB Output is correct
17 Correct 244 ms 32092 KB Output is correct
18 Correct 242 ms 31760 KB Output is correct
19 Correct 241 ms 31848 KB Output is correct
20 Correct 250 ms 28228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 11 ms 2284 KB Output is correct
7 Correct 247 ms 31908 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 0 ms 336 KB Output is correct
12 Correct 9 ms 1484 KB Output is correct
13 Correct 229 ms 28068 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 5 ms 972 KB Output is correct
17 Correct 112 ms 14104 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 73 ms 9096 KB Output is correct
21 Correct 251 ms 31940 KB Output is correct
22 Correct 236 ms 31564 KB Output is correct
23 Correct 260 ms 31900 KB Output is correct
24 Correct 233 ms 28192 KB Output is correct
25 Correct 106 ms 14020 KB Output is correct
26 Correct 104 ms 14120 KB Output is correct
27 Correct 240 ms 31940 KB Output is correct
28 Correct 230 ms 28200 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 0 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 0 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 11 ms 1376 KB Output is correct
37 Correct 245 ms 28068 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 0 ms 332 KB Output is correct
40 Correct 11 ms 2252 KB Output is correct
41 Correct 230 ms 31912 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 5 ms 844 KB Output is correct
45 Correct 110 ms 14108 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 0 ms 332 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 68 ms 9060 KB Output is correct
50 Correct 253 ms 32072 KB Output is correct
51 Correct 228 ms 31940 KB Output is correct
52 Correct 241 ms 31948 KB Output is correct
53 Correct 109 ms 14360 KB Output is correct
54 Correct 112 ms 14024 KB Output is correct
55 Correct 252 ms 31816 KB Output is correct
56 Correct 248 ms 31956 KB Output is correct
57 Correct 117 ms 14404 KB Output is correct
58 Correct 1 ms 332 KB Output is correct
59 Correct 0 ms 332 KB Output is correct
60 Correct 5 ms 844 KB Output is correct
61 Correct 107 ms 14120 KB Output is correct
62 Correct 1 ms 332 KB Output is correct
63 Correct 1 ms 332 KB Output is correct
64 Correct 1 ms 332 KB Output is correct
65 Correct 63 ms 9116 KB Output is correct
66 Correct 107 ms 14108 KB Output is correct
67 Correct 105 ms 14020 KB Output is correct
68 Correct 112 ms 14020 KB Output is correct
69 Correct 136 ms 14240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 11 ms 2284 KB Output is correct
7 Correct 247 ms 31908 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 0 ms 336 KB Output is correct
12 Correct 9 ms 1484 KB Output is correct
13 Correct 229 ms 28068 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 5 ms 972 KB Output is correct
17 Correct 112 ms 14104 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 73 ms 9096 KB Output is correct
21 Correct 251 ms 31940 KB Output is correct
22 Correct 236 ms 31564 KB Output is correct
23 Correct 260 ms 31900 KB Output is correct
24 Correct 233 ms 28192 KB Output is correct
25 Correct 106 ms 14020 KB Output is correct
26 Correct 104 ms 14120 KB Output is correct
27 Correct 240 ms 31940 KB Output is correct
28 Correct 230 ms 28200 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 0 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 0 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 11 ms 1376 KB Output is correct
37 Correct 245 ms 28068 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 0 ms 332 KB Output is correct
40 Correct 11 ms 2252 KB Output is correct
41 Correct 230 ms 31912 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 5 ms 844 KB Output is correct
45 Correct 110 ms 14108 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 0 ms 332 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 68 ms 9060 KB Output is correct
50 Correct 253 ms 32072 KB Output is correct
51 Correct 228 ms 31940 KB Output is correct
52 Correct 241 ms 31948 KB Output is correct
53 Correct 109 ms 14360 KB Output is correct
54 Correct 112 ms 14024 KB Output is correct
55 Correct 252 ms 31816 KB Output is correct
56 Correct 248 ms 31956 KB Output is correct
57 Correct 117 ms 14404 KB Output is correct
58 Correct 1 ms 332 KB Output is correct
59 Correct 1 ms 332 KB Output is correct
60 Correct 1 ms 332 KB Output is correct
61 Correct 63 ms 9120 KB Output is correct
62 Correct 253 ms 31944 KB Output is correct
63 Correct 241 ms 31428 KB Output is correct
64 Correct 280 ms 31940 KB Output is correct
65 Correct 1 ms 332 KB Output is correct
66 Correct 67 ms 9156 KB Output is correct
67 Correct 274 ms 31916 KB Output is correct
68 Correct 242 ms 31940 KB Output is correct
69 Correct 245 ms 32196 KB Output is correct
70 Correct 1 ms 332 KB Output is correct
71 Correct 0 ms 332 KB Output is correct
72 Correct 1 ms 332 KB Output is correct
73 Correct 63 ms 9156 KB Output is correct
74 Correct 244 ms 32092 KB Output is correct
75 Correct 242 ms 31760 KB Output is correct
76 Correct 241 ms 31848 KB Output is correct
77 Correct 250 ms 28228 KB Output is correct
78 Correct 1 ms 332 KB Output is correct
79 Correct 0 ms 332 KB Output is correct
80 Correct 5 ms 844 KB Output is correct
81 Correct 107 ms 14120 KB Output is correct
82 Correct 1 ms 332 KB Output is correct
83 Correct 1 ms 332 KB Output is correct
84 Correct 1 ms 332 KB Output is correct
85 Correct 63 ms 9116 KB Output is correct
86 Correct 107 ms 14108 KB Output is correct
87 Correct 105 ms 14020 KB Output is correct
88 Correct 112 ms 14020 KB Output is correct
89 Correct 136 ms 14240 KB Output is correct
90 Correct 1 ms 332 KB Output is correct
91 Correct 1 ms 332 KB Output is correct
92 Correct 5 ms 844 KB Output is correct
93 Correct 107 ms 14108 KB Output is correct
94 Correct 1 ms 332 KB Output is correct
95 Correct 1 ms 332 KB Output is correct
96 Correct 1 ms 332 KB Output is correct
97 Correct 31 ms 3740 KB Output is correct
98 Correct 102 ms 14104 KB Output is correct
99 Correct 109 ms 14104 KB Output is correct
100 Correct 109 ms 14112 KB Output is correct
101 Correct 135 ms 14104 KB Output is correct
102 Correct 106 ms 14124 KB Output is correct
103 Correct 128 ms 14108 KB Output is correct
104 Correct 108 ms 14116 KB Output is correct
105 Correct 100 ms 14120 KB Output is correct