답안 #419979

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
419979 2021-06-07T20:40:08 Z ioi 슈퍼트리 잇기 (IOI20_supertrees) C++14
100 / 100
309 ms 26140 KB
#include "supertrees.h"
#include <vector>
#include<bits/stdc++.h>
using namespace std ;
const int N = 1002;
int dsu[N] , sz[N];

int get(int u){
    if(u == dsu[u])
        return u ;

    return dsu[u] = get(dsu[u]);



}

void connect(int u , int v){


    u = get(u) , v = get(v);
    if(u == v)return ;

    if(sz[u] > sz[v])swap(u , v);

    sz[v] += sz[u];

    dsu[u] = v ;



}

bool same(int u , int v){
    u = get(u) , v = get(v);

    return u == v ;

}
vector<int> v , adj[N];

int construct(vector<vector<int>> p) {
	int n = p.size();
	vector<vector<int>> answer (n , vector<int> (n , 0));
    for(int i = 0 ; i < n ; i ++)sz[i] = 1 , dsu[i] = i ;
    int cnt[n] = {} , cnt1[n] = {} , cnt3[n] = {};

    for(int i = 0 ; i < n ; i ++){
        for(int j = 0 ; j < n ; j ++){
            if(p[i][j])connect(i , j);
            if(i == j)continue;

            cnt[i] += (p[i][j] == 2 );cnt1[i] += (p[i][j] == 1);cnt3[i] += (p[i][j] == 3);

            if(p[i][j] == 1 )adj[i].push_back(j) ;// , adj[j].push_back(i);

        }
    }
    map<int, set<int> > mp ;


    for(int i = 0 ; i < n ; i ++){
        for(int j = 0 ; j < n ;j ++)
        {
           // if(p[i][j] == 0&& same(i , j) || (p[i][j] && sz[get(dsu[i])] == 2))return 0 ;
             if(p[i][j] && i != j)mp[dsu[i]].insert(i) , mp[dsu[i]].insert(j);

        }
    }

    int vis[n] = {};

    for(auto f : mp){

        int two = 0 , thr = 0 ;

        for(auto it : f.second){

            for(auto it2 : f.second){

                if(p[it][it2] == 0)return 0 ;

            }

        }
       // cout << "FF\n";
        vector<int> cycle   ;
        map<int , int > inc ;

        for(auto ff : f.second){
            if(cnt[ff])two ++ ;
            if(cnt3[ff])thr ++ ;

            if(!vis[ff]){
                vis[ff] ++ ;
                cycle.push_back(ff);
                inc[ff] ++ ;

                for(auto it : adj[ff]){
                    vis[it] ++ ;
                }

            }
        }
        if(two && thr)return 0 ;


            if(cycle.size() == 1){

                int last = cycle[0];
                vector<int> v ;
                v.push_back(cycle[0]);
                for(auto it : adj[cycle[0]]){
                  //  cout << "last " << last << " " << it << endl ;
                    answer[it][last] = answer[last][it] = 1 ;
                    last = it ;
                    v.push_back(it);


                }

                for(int i = 0 ; i < v.size() ; i ++){
                    for(int j = i + 1 ; j < v.size() ; j ++){
                        if(p[v[i]][v[j]] != 1)
                            return 0 ;

                    }
                }



            }
            else if(two) {
                if(cycle.size() < 3 )return 0 ;

                int last = cycle[0];

                for(int i = 1 ; i < cycle.size() ; i ++){

                    int curr = cycle[i];

                    answer[last][curr] = answer[curr][last] = 1 ;

                    last = curr ;


                }

                answer[cycle[0]][last] = answer[last][cycle[0]] = 1 ;


                for(auto ff : cycle){
                    int last = ff ;
                    for(auto it : adj[ff]){

                        answer[it][last] = answer[last][it] = 1 ;

                        last = it ;


                        for(auto it2 : adj[it]){
                            if(inc.count(it2) && it2 != ff)return 0 ;
                        }


                    }


                }

            }
            else {



                if(cycle.size() < 4 )return 0 ;

                int last = cycle[0];

                for(int i = 1 ; i < cycle.size() ; i ++){

                    int curr = cycle[i];

                    answer[last][curr] = answer[curr][last] = 1 ;

                    last = curr ;


                }

                answer[cycle[0]][last] = answer[last][cycle[0]] = 1 ;

                int md = cycle[cycle.size() / 2];

                int st = cycle[0];

                answer[md][st] = answer[st][md] = 1 ;


                for(auto it : cycle){

                    for(auto it2 : cycle){
                        if(p[it][it2] != 3)return 0 ;
                    }

                }


                for(auto ff : cycle){
                    int last = ff ;
                    for(auto it : adj[ff]){

                        answer[it][last] = answer[last][it] = 1 ;

                        last = it ;


                        for(auto it2 : adj[it]){
                            if(inc.count(it2) && it2 != ff)return 0 ;
                        }


                    }


                }



            }







    }


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

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:122:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  122 |                 for(int i = 0 ; i < v.size() ; i ++){
      |                                 ~~^~~~~~~~~~
supertrees.cpp:123:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  123 |                     for(int j = i + 1 ; j < v.size() ; j ++){
      |                                         ~~^~~~~~~~~~
supertrees.cpp:138:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  138 |                 for(int i = 1 ; i < cycle.size() ; i ++){
      |                                 ~~^~~~~~~~~~~~~~
supertrees.cpp:180:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  180 |                 for(int i = 1 ; i < cycle.size() ; i ++){
      |                                 ~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 13 ms 1360 KB Output is correct
7 Correct 309 ms 26140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 13 ms 1360 KB Output is correct
7 Correct 309 ms 26140 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 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 11 ms 1120 KB Output is correct
13 Correct 229 ms 22036 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 7 ms 864 KB Output is correct
17 Correct 168 ms 14628 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 62 ms 5788 KB Output is correct
21 Correct 252 ms 22548 KB Output is correct
22 Correct 237 ms 22128 KB Output is correct
23 Correct 293 ms 24112 KB Output is correct
24 Correct 227 ms 22048 KB Output is correct
25 Correct 112 ms 12700 KB Output is correct
26 Correct 105 ms 12196 KB Output is correct
27 Correct 288 ms 25096 KB Output is correct
28 Correct 227 ms 22088 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 1 ms 220 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 10 ms 1100 KB Output is correct
9 Correct 227 ms 22076 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 12 ms 1200 KB Output is correct
13 Correct 308 ms 22052 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 7 ms 716 KB Output is correct
17 Correct 158 ms 12324 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 59 ms 5784 KB Output is correct
22 Correct 241 ms 22040 KB Output is correct
23 Correct 246 ms 22100 KB Output is correct
24 Correct 286 ms 22036 KB Output is correct
25 Correct 99 ms 12068 KB Output is correct
26 Correct 109 ms 12200 KB Output is correct
27 Correct 227 ms 22052 KB Output is correct
28 Correct 288 ms 22212 KB Output is correct
29 Correct 106 ms 12100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 58 ms 5788 KB Output is correct
5 Correct 250 ms 22656 KB Output is correct
6 Correct 232 ms 22144 KB Output is correct
7 Correct 292 ms 24140 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 61 ms 5792 KB Output is correct
10 Correct 241 ms 22036 KB Output is correct
11 Correct 226 ms 22084 KB Output is correct
12 Correct 282 ms 22064 KB Output is correct
13 Correct 1 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 Correct 58 ms 5780 KB Output is correct
17 Correct 264 ms 22180 KB Output is correct
18 Correct 240 ms 22180 KB Output is correct
19 Correct 230 ms 22212 KB Output is correct
20 Correct 226 ms 22040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 13 ms 1360 KB Output is correct
7 Correct 309 ms 26140 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 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 11 ms 1120 KB Output is correct
13 Correct 229 ms 22036 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 7 ms 864 KB Output is correct
17 Correct 168 ms 14628 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 62 ms 5788 KB Output is correct
21 Correct 252 ms 22548 KB Output is correct
22 Correct 237 ms 22128 KB Output is correct
23 Correct 293 ms 24112 KB Output is correct
24 Correct 227 ms 22048 KB Output is correct
25 Correct 112 ms 12700 KB Output is correct
26 Correct 105 ms 12196 KB Output is correct
27 Correct 288 ms 25096 KB Output is correct
28 Correct 227 ms 22088 KB Output is correct
29 Correct 0 ms 204 KB Output is correct
30 Correct 0 ms 204 KB Output is correct
31 Correct 0 ms 204 KB Output is correct
32 Correct 1 ms 220 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 0 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 10 ms 1100 KB Output is correct
37 Correct 227 ms 22076 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 0 ms 204 KB Output is correct
40 Correct 12 ms 1200 KB Output is correct
41 Correct 308 ms 22052 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 7 ms 716 KB Output is correct
45 Correct 158 ms 12324 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 59 ms 5784 KB Output is correct
50 Correct 241 ms 22040 KB Output is correct
51 Correct 246 ms 22100 KB Output is correct
52 Correct 286 ms 22036 KB Output is correct
53 Correct 99 ms 12068 KB Output is correct
54 Correct 109 ms 12200 KB Output is correct
55 Correct 227 ms 22052 KB Output is correct
56 Correct 288 ms 22212 KB Output is correct
57 Correct 106 ms 12100 KB Output is correct
58 Correct 0 ms 204 KB Output is correct
59 Correct 1 ms 204 KB Output is correct
60 Correct 7 ms 868 KB Output is correct
61 Correct 171 ms 14248 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 Correct 57 ms 5784 KB Output is correct
66 Correct 111 ms 12180 KB Output is correct
67 Correct 112 ms 12228 KB Output is correct
68 Correct 103 ms 12184 KB Output is correct
69 Correct 99 ms 12060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 13 ms 1360 KB Output is correct
7 Correct 309 ms 26140 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 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 11 ms 1120 KB Output is correct
13 Correct 229 ms 22036 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 7 ms 864 KB Output is correct
17 Correct 168 ms 14628 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 62 ms 5788 KB Output is correct
21 Correct 252 ms 22548 KB Output is correct
22 Correct 237 ms 22128 KB Output is correct
23 Correct 293 ms 24112 KB Output is correct
24 Correct 227 ms 22048 KB Output is correct
25 Correct 112 ms 12700 KB Output is correct
26 Correct 105 ms 12196 KB Output is correct
27 Correct 288 ms 25096 KB Output is correct
28 Correct 227 ms 22088 KB Output is correct
29 Correct 0 ms 204 KB Output is correct
30 Correct 0 ms 204 KB Output is correct
31 Correct 0 ms 204 KB Output is correct
32 Correct 1 ms 220 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 0 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 10 ms 1100 KB Output is correct
37 Correct 227 ms 22076 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 0 ms 204 KB Output is correct
40 Correct 12 ms 1200 KB Output is correct
41 Correct 308 ms 22052 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 7 ms 716 KB Output is correct
45 Correct 158 ms 12324 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 59 ms 5784 KB Output is correct
50 Correct 241 ms 22040 KB Output is correct
51 Correct 246 ms 22100 KB Output is correct
52 Correct 286 ms 22036 KB Output is correct
53 Correct 99 ms 12068 KB Output is correct
54 Correct 109 ms 12200 KB Output is correct
55 Correct 227 ms 22052 KB Output is correct
56 Correct 288 ms 22212 KB Output is correct
57 Correct 106 ms 12100 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 0 ms 204 KB Output is correct
60 Correct 1 ms 204 KB Output is correct
61 Correct 58 ms 5788 KB Output is correct
62 Correct 250 ms 22656 KB Output is correct
63 Correct 232 ms 22144 KB Output is correct
64 Correct 292 ms 24140 KB Output is correct
65 Correct 1 ms 204 KB Output is correct
66 Correct 61 ms 5792 KB Output is correct
67 Correct 241 ms 22036 KB Output is correct
68 Correct 226 ms 22084 KB Output is correct
69 Correct 282 ms 22064 KB Output is correct
70 Correct 1 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 Correct 58 ms 5780 KB Output is correct
74 Correct 264 ms 22180 KB Output is correct
75 Correct 240 ms 22180 KB Output is correct
76 Correct 230 ms 22212 KB Output is correct
77 Correct 226 ms 22040 KB Output is correct
78 Correct 0 ms 204 KB Output is correct
79 Correct 1 ms 204 KB Output is correct
80 Correct 7 ms 868 KB Output is correct
81 Correct 171 ms 14248 KB Output is correct
82 Correct 0 ms 204 KB Output is correct
83 Correct 0 ms 204 KB Output is correct
84 Correct 1 ms 204 KB Output is correct
85 Correct 57 ms 5784 KB Output is correct
86 Correct 111 ms 12180 KB Output is correct
87 Correct 112 ms 12228 KB Output is correct
88 Correct 103 ms 12184 KB Output is correct
89 Correct 99 ms 12060 KB Output is correct
90 Correct 0 ms 204 KB Output is correct
91 Correct 1 ms 204 KB Output is correct
92 Correct 7 ms 844 KB Output is correct
93 Correct 172 ms 13472 KB Output is correct
94 Correct 1 ms 204 KB Output is correct
95 Correct 0 ms 204 KB Output is correct
96 Correct 0 ms 204 KB Output is correct
97 Correct 29 ms 3228 KB Output is correct
98 Correct 112 ms 12196 KB Output is correct
99 Correct 114 ms 12276 KB Output is correct
100 Correct 109 ms 12228 KB Output is correct
101 Correct 100 ms 12068 KB Output is correct
102 Correct 175 ms 12320 KB Output is correct
103 Correct 184 ms 14176 KB Output is correct
104 Correct 186 ms 16656 KB Output is correct
105 Correct 163 ms 14148 KB Output is correct