답안 #405282

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
405282 2021-05-16T07:07:09 Z ngrace 슈퍼트리 잇기 (IOI20_supertrees) C++14
100 / 100
281 ms 24188 KB
#include "supertrees.h"
#include <vector>

#include <bits/stdc++.h>

using namespace std;

int construct(std::vector<std::vector<int>> p) {
	int n = p.size();
	std::vector<std::vector<int>> answer;
	for (int i = 0; i < n; i++) {
		std::vector<int> row;
		row.resize(n);
		answer.push_back(row);
	}
	
	bool onesOnlyOnDiag=true;
	vector<int> present(4,0);
	for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(i!=j && p[i][j]==1){
                onesOnlyOnDiag=false;
            }
            present[p[i][j]]=1;
        }
    }
    
	bool poss=true;
	
    if(present[3]){//sub 6 - no additional constraints
        poss=false;
    }
    else if(present[2]&&present[1] && !onesOnlyOnDiag){//sub 5&4 - 0's,1's and 2's, 4 there is a solution, 5 there may not be
        map<vector<int>,vector<int>> cycles;
        map<vector<int>,vector<int>> lines;
        set<vector<int>> mix;
        vector<int> in_mix;
        
        for(int i=0;i<n;i++){
            bool ones=true;
            bool twos=true;
            for(int j=0;j<n;j++){
                if(p[i][j]==1 && i!=j){
                    twos=false;
                }
                if(p[i][j]==2){
                    ones=false;
                }
            }
            if(twos){
                p[i][i]=2;
                if(cycles.find(p[i])==cycles.end()){
                    vector<int> tmp(n,0);
                    cycles[p[i]]=tmp;
                }
                cycles[p[i]][i]=2;
            }
            else if(ones){
                if(lines.find(p[i])==lines.end()){
                    vector<int> tmp(n,0);
                    lines[p[i]]=tmp;
                }
                lines[p[i]][i]=1;
            }
            else{
                in_mix.push_back(i);
                vector<int> tmp(n,0);
                for(int j=0;j<n;j++){
                    if(p[i][j]>0){
                        tmp[j]=1;
                    }
                }
                mix.insert(tmp);
            }
        }
        
        sort(in_mix.begin(),in_mix.end());
        
        for(auto&it:cycles){
            vector<int> key = it.first;
            vector<int> val = it.second;
            
            /**if(key!=val){
                poss=false;
                break;
            }**/
            
            bool m=false;
            int count=0;
            int last=-1;
            int first=-1;
            for(int i=0;i<n;i++){
                if(key[i]==2){
                    if(in_mix.size()>0){
                        auto lb=lower_bound(in_mix.begin(),in_mix.end(),i);
                        if((*lb)==i){
                            m=true;
                            break;
                        }
                    }
                    
                    count++;
                    if(last==-1){
                        last=i;
                        first=i;
                    }
                    else{
                        answer[i][last]=1;
                        answer[last][i]=1;
                        last=i;
                    }
                }
            }
            
            if(m){
                continue;
            }
            
            if(key!=val || count==2){
                poss=false;
                break;
            }
            
            if(first!=last && count>0){
                answer[first][last]=1;
                answer[last][first]=1;
            }
        }
        
        for(auto&it:lines){
            vector<int> key = it.first;
            vector<int> val = it.second;
            
            if(key!=val){
                poss=false;
                break;
            }
            
            int last=-1;
            for(int i=0;i<n;i++){
                if(key[i]){
                    if(last==-1){
                        last=i;
                    }
                    else{
                        answer[i][last]=1;
                        answer[last][i]=1;
                    }
                }
            }
        }
        
        for(auto&it:mix){
            if(!poss){
                break;
            }
            
            vector<int> in_lines(1,-1);
            vector<vector<int>> mlines(1,vector<int>(0,0));
            vector<int> cycle;
                        
            for(int i=0;i<n;i++){//put in lines or cycle
                if(it[i]){
                    if(in_lines.size()>0){
                        sort(in_lines.begin(),in_lines.end());
                        auto lb=lower_bound(in_lines.begin(),in_lines.end(),i);
                        if((*lb)==i){
                            continue;
                        }
                    }
                    
                    bool ones=false;
                    for(int j=0;j<n;j++){
                        if(i!=j){
                            if(p[i][j]==1){
                                ones=true;
                                mlines[mlines.size()-1].push_back(j);
                                in_lines.push_back(j);
                            }
                        }
                    }
                    if(ones){
                        mlines[mlines.size()-1].push_back(i);
                        in_lines.push_back(i);
                        vector<int> tmp;
                        mlines.push_back(tmp);
                    }
                    else{
                        cycle.push_back(i);
                    }
                }
            }
            
            if(mlines.size()>0){
                mlines.pop_back();
            }
            
            for(vector<int>&pt:mlines){
                if(pt.size()>0){
                    cycle.push_back(pt[0]);
                }
            }
            
            if(cycle.size()>2){
                answer[cycle[0]][cycle[cycle.size()-1]]=1;
                answer[cycle[cycle.size()-1]][cycle[0]]=1;
                for(int i=0;i<cycle.size()-1;i++){
                    answer[cycle[i]][cycle[i+1]]=1;
                    answer[cycle[i+1]][cycle[i]]=1;
                }
            }
            else{
                poss=false;
            }
            
            
            for(vector<int>& pt:mlines){
                for(int i=1;i<pt.size();i++){
                    for(int j=1;j<pt.size();j++){
                        if(p[pt[i]][pt[j]]!=1){
                            poss=false;
                        }
                    }
                    answer[pt[i]][pt[0]]=1;
                    answer[pt[0]][pt[i]]=1;
                }
            }
        }
    }
    else if(present[2]){//sub 3 - 2's and 0's
        map<vector<int>,vector<int>> cycles;
        
        for(int i=0;i<n;i++){
            p[i][i]=2;
            if(cycles.find(p[i])==cycles.end()){
                vector<int> tmp(n,0);
                cycles[p[i]]=tmp;
            }
            cycles[p[i]][i]=2;
        }
        
        for(auto&it:cycles){
            vector<int> key = it.first;
            vector<int> val = it.second;
            
            if(key!=val){
                poss=false;
                break;
            }
            
            
            int count=0;
            int last=-1;
            int first=-1;
            for(int i=0;i<n;i++){
                if(key[i]==2){
                    count++;
                    if(last==-1){
                        last=i;
                        first=i;
                    }
                    else{
                        answer[i][last]=1;
                        answer[last][i]=1;
                        last=i;
                    }
                }
            }
            
            if(key!=val || count==2){
                poss=false;
                break;
            }
            
            if(first!=last){
                answer[first][last]=1;
                answer[last][first]=1;
            }
        }
    }
    else if(present[1] && present[0]){//sub 2 - 1's and 0's
        map<vector<int>,vector<int>> lines;
        
        for(int i=0;i<n;i++){
            if(lines.find(p[i])==lines.end()){
                vector<int> tmp(n,0);
                lines[p[i]]=tmp;
            }
            lines[p[i]][i]=1;
        }
        
        for(auto&it:lines){
            vector<int> key = it.first;
            vector<int> val = it.second;
            
            if(key!=val){
                poss=false;
                break;
            }
            
            int last=-1;
            for(int i=0;i<n;i++){
                if(key[i]){
                    if(last==-1){
                        last=i;
                    }
                    else{
                        answer[i][last]=1;
                        answer[last][i]=1;
                    }
                }
            }
        }
    }
    else{//sub 1 - just 1's
        for(int i=0;i<n-1;i++){
            answer[i][i+1]=1;
            answer[i+1][i]=1;
        }
    }
    
    
    if(poss){
        build(answer);
        return 1;
    }
    else{
        return 0;
    }
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:207:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  207 |                 for(int i=0;i<cycle.size()-1;i++){
      |                             ~^~~~~~~~~~~~~~~
supertrees.cpp:218:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  218 |                 for(int i=1;i<pt.size();i++){
      |                             ~^~~~~~~~~~
supertrees.cpp:219:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  219 |                     for(int j=1;j<pt.size();j++){
      |                                 ~^~~~~~~~~~
# 결과 실행 시간 메모리 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 292 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 10 ms 1164 KB Output is correct
7 Correct 224 ms 23916 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 292 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 10 ms 1164 KB Output is correct
7 Correct 224 ms 23916 KB Output is correct
8 Correct 1 ms 292 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 11 ms 1228 KB Output is correct
13 Correct 248 ms 24136 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 5 ms 1100 KB Output is correct
17 Correct 113 ms 21996 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 67 ms 6252 KB Output is correct
21 Correct 281 ms 23916 KB Output is correct
22 Correct 241 ms 24004 KB Output is correct
23 Correct 239 ms 23912 KB Output is correct
24 Correct 280 ms 23996 KB Output is correct
25 Correct 115 ms 14148 KB Output is correct
26 Correct 108 ms 17272 KB Output is correct
27 Correct 236 ms 23928 KB Output is correct
28 Correct 257 ms 24188 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 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 11 ms 1216 KB Output is correct
9 Correct 247 ms 24056 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 288 KB Output is correct
12 Correct 10 ms 1196 KB Output is correct
13 Correct 242 ms 23924 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 5 ms 1196 KB Output is correct
17 Correct 123 ms 22008 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 59 ms 6212 KB Output is correct
22 Correct 229 ms 23916 KB Output is correct
23 Correct 275 ms 24076 KB Output is correct
24 Correct 241 ms 23960 KB Output is correct
25 Correct 144 ms 21572 KB Output is correct
26 Correct 98 ms 14076 KB Output is correct
27 Correct 231 ms 23924 KB Output is correct
28 Correct 230 ms 24004 KB Output is correct
29 Correct 128 ms 21608 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 58 ms 6212 KB Output is correct
5 Correct 224 ms 23928 KB Output is correct
6 Correct 279 ms 23924 KB Output is correct
7 Correct 235 ms 24004 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 58 ms 6216 KB Output is correct
10 Correct 231 ms 23932 KB Output is correct
11 Correct 230 ms 23928 KB Output is correct
12 Correct 242 ms 23920 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 58 ms 6252 KB Output is correct
17 Correct 226 ms 23976 KB Output is correct
18 Correct 237 ms 23904 KB Output is correct
19 Correct 226 ms 23940 KB Output is correct
20 Correct 248 ms 24004 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 292 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 10 ms 1164 KB Output is correct
7 Correct 224 ms 23916 KB Output is correct
8 Correct 1 ms 292 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 11 ms 1228 KB Output is correct
13 Correct 248 ms 24136 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 5 ms 1100 KB Output is correct
17 Correct 113 ms 21996 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 67 ms 6252 KB Output is correct
21 Correct 281 ms 23916 KB Output is correct
22 Correct 241 ms 24004 KB Output is correct
23 Correct 239 ms 23912 KB Output is correct
24 Correct 280 ms 23996 KB Output is correct
25 Correct 115 ms 14148 KB Output is correct
26 Correct 108 ms 17272 KB Output is correct
27 Correct 236 ms 23928 KB Output is correct
28 Correct 257 ms 24188 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 11 ms 1216 KB Output is correct
37 Correct 247 ms 24056 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 1 ms 288 KB Output is correct
40 Correct 10 ms 1196 KB Output is correct
41 Correct 242 ms 23924 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 5 ms 1196 KB Output is correct
45 Correct 123 ms 22008 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 59 ms 6212 KB Output is correct
50 Correct 229 ms 23916 KB Output is correct
51 Correct 275 ms 24076 KB Output is correct
52 Correct 241 ms 23960 KB Output is correct
53 Correct 144 ms 21572 KB Output is correct
54 Correct 98 ms 14076 KB Output is correct
55 Correct 231 ms 23924 KB Output is correct
56 Correct 230 ms 24004 KB Output is correct
57 Correct 128 ms 21608 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 1 ms 204 KB Output is correct
60 Correct 7 ms 972 KB Output is correct
61 Correct 148 ms 18024 KB Output is correct
62 Correct 1 ms 204 KB Output is correct
63 Correct 1 ms 204 KB Output is correct
64 Correct 1 ms 204 KB Output is correct
65 Correct 59 ms 6244 KB Output is correct
66 Correct 100 ms 14196 KB Output is correct
67 Correct 124 ms 14184 KB Output is correct
68 Correct 102 ms 14564 KB Output is correct
69 Correct 123 ms 21572 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 292 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 10 ms 1164 KB Output is correct
7 Correct 224 ms 23916 KB Output is correct
8 Correct 1 ms 292 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 11 ms 1228 KB Output is correct
13 Correct 248 ms 24136 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 5 ms 1100 KB Output is correct
17 Correct 113 ms 21996 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 67 ms 6252 KB Output is correct
21 Correct 281 ms 23916 KB Output is correct
22 Correct 241 ms 24004 KB Output is correct
23 Correct 239 ms 23912 KB Output is correct
24 Correct 280 ms 23996 KB Output is correct
25 Correct 115 ms 14148 KB Output is correct
26 Correct 108 ms 17272 KB Output is correct
27 Correct 236 ms 23928 KB Output is correct
28 Correct 257 ms 24188 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 11 ms 1216 KB Output is correct
37 Correct 247 ms 24056 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 1 ms 288 KB Output is correct
40 Correct 10 ms 1196 KB Output is correct
41 Correct 242 ms 23924 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 5 ms 1196 KB Output is correct
45 Correct 123 ms 22008 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 59 ms 6212 KB Output is correct
50 Correct 229 ms 23916 KB Output is correct
51 Correct 275 ms 24076 KB Output is correct
52 Correct 241 ms 23960 KB Output is correct
53 Correct 144 ms 21572 KB Output is correct
54 Correct 98 ms 14076 KB Output is correct
55 Correct 231 ms 23924 KB Output is correct
56 Correct 230 ms 24004 KB Output is correct
57 Correct 128 ms 21608 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 1 ms 204 KB Output is correct
60 Correct 1 ms 204 KB Output is correct
61 Correct 58 ms 6212 KB Output is correct
62 Correct 224 ms 23928 KB Output is correct
63 Correct 279 ms 23924 KB Output is correct
64 Correct 235 ms 24004 KB Output is correct
65 Correct 1 ms 204 KB Output is correct
66 Correct 58 ms 6216 KB Output is correct
67 Correct 231 ms 23932 KB Output is correct
68 Correct 230 ms 23928 KB Output is correct
69 Correct 242 ms 23920 KB Output is correct
70 Correct 1 ms 204 KB Output is correct
71 Correct 1 ms 204 KB Output is correct
72 Correct 1 ms 204 KB Output is correct
73 Correct 58 ms 6252 KB Output is correct
74 Correct 226 ms 23976 KB Output is correct
75 Correct 237 ms 23904 KB Output is correct
76 Correct 226 ms 23940 KB Output is correct
77 Correct 248 ms 24004 KB Output is correct
78 Correct 1 ms 204 KB Output is correct
79 Correct 1 ms 204 KB Output is correct
80 Correct 7 ms 972 KB Output is correct
81 Correct 148 ms 18024 KB Output is correct
82 Correct 1 ms 204 KB Output is correct
83 Correct 1 ms 204 KB Output is correct
84 Correct 1 ms 204 KB Output is correct
85 Correct 59 ms 6244 KB Output is correct
86 Correct 100 ms 14196 KB Output is correct
87 Correct 124 ms 14184 KB Output is correct
88 Correct 102 ms 14564 KB Output is correct
89 Correct 123 ms 21572 KB Output is correct
90 Correct 1 ms 204 KB Output is correct
91 Correct 1 ms 292 KB Output is correct
92 Correct 5 ms 780 KB Output is correct
93 Correct 100 ms 14068 KB Output is correct
94 Correct 1 ms 204 KB Output is correct
95 Correct 1 ms 292 KB Output is correct
96 Correct 1 ms 204 KB Output is correct
97 Correct 26 ms 3788 KB Output is correct
98 Correct 98 ms 14068 KB Output is correct
99 Correct 96 ms 14060 KB Output is correct
100 Correct 95 ms 14060 KB Output is correct
101 Correct 95 ms 14068 KB Output is correct
102 Correct 93 ms 14060 KB Output is correct
103 Correct 95 ms 14072 KB Output is correct
104 Correct 97 ms 14056 KB Output is correct
105 Correct 109 ms 14056 KB Output is correct