Submission #302859

# Submission time Handle Problem Language Result Execution time Memory
302859 2020-09-19T09:52:04 Z whaleee Connecting Supertrees (IOI20_supertrees) C++14
100 / 100
593 ms 69112 KB
#include "supertrees.h"
#include <bits/stdc++.h>
 
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
 
using namespace std;
 
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

struct trip {
    int ones=0,twos=0,threes=0;
};

int construct(vector<vector<int>> p) {
	int n = p.size();
	vector<vector<int>> answer(n);
	forn(i,n) {
		vi empty(n,0);
		answer[i]= empty;
	}
	
    map<int, set<int>>node_to_friends;
	vector<set<int>> clusters;
	vector<trip> class_type(n,{0,0,0});
	vi cluster_vec(n,-1);
	int cnt = 0;
	forn(i, n) {
		int cnt0 = 0;
		int cnt1 = 0;
		int cnt2 = 0;
		int cnt3 = 0;

		if (cluster_vec[i] == -1) {
			cluster_vec[i] = cnt;
			cnt++;
		}
		int cluster_id = cluster_vec[i];

		forn(j,n) {
			if (p[i][j] == 0) {
				cnt0++;
			}
			else if (p[i][j] == 1) {
				cnt1++;
				if (cluster_vec[j] == -1) {cluster_vec[j] = cluster_id;}
				else if (cluster_vec[j] != cluster_id) {return 0;} // contr
			}
			else if (p[i][j] == 2) {
				cnt2++;
				if (cluster_vec[j] == -1) {cluster_vec[j] = cluster_id;}
				else if (cluster_vec[j] != cluster_id) {return 0;} // contr
			}
			else if (p[i][j] == 3) {
				cnt3++;
				if (cluster_vec[j] == -1) {cluster_vec[j] = cluster_id;}
				else if (cluster_vec[j] != cluster_id) {return 0;} // contr
				
			}
            else {assert(0);}
		}

		if (cnt1 == 1 && cnt0 == n-1) {
			continue;
		}
		else {
            // check that there is connection
            forn(j,i) {
                if (cluster_vec[j] == cluster_id  && p[i][j] == 0) {
                    return 0;
                }
            if (p[i][j] == 1) {class_type[i].ones++; class_type[j].ones++;node_to_friends[i].insert(j);node_to_friends[j].insert(i);}
            if (p[i][j] == 2) {class_type[i].twos++; class_type[j].twos++;}
            if (p[i][j] == 3) {class_type[i].threes++; class_type[j].threes++;}
            }
        }
	}

	forn(i,cnt) {
		map<pair<int, pair<int,int>>, set<int>> combo_to_cnt;
        int cnt = 0;
		forn(j, n) {
			if (cluster_vec[j] == i) {
				// combo_to_cnt[{class_type[j].ones, {class_type[j].twos,class_type[j].threes}}]++;
                combo_to_cnt[{class_type[j].ones, {class_type[j].twos,class_type[j].threes}}].insert(j);
                cnt++;
			}   
		}
        if (cnt  <= 1) {
            continue;
        }
        vi circle;
        int to_push_start = -1;
        int to_push_end = -1;
        int two_pure = -1;
        int three_pure = -1;
        for(const auto &myPair : combo_to_cnt) {
            if (myPair.fi.fi == 0 && myPair.fi.se.fi == 0 && myPair.fi.se.se >0 ) {
                three_pure = myPair.se.size();
            }
            if (myPair.fi.fi == 0 && myPair.fi.se.fi > 0) {
                two_pure = myPair.se.size();
                int cnt = 0;
                int cur_node = -1;
                for (int node: myPair.se) {
                    if (cnt == 0) {
                        // circle.pb(node);
                        to_push_start = node;
                    }
                    else {
                        answer[node][cur_node] = 1;
                        answer[cur_node][node] = 1;
                    }
                    
                    cur_node = node;
                    cnt++;
                    if (cnt == myPair.se.size()) {
                        // circle.pb(node);
                        to_push_end = node;
                    }
                }
                continue;
            }
            
            
            int cnt = 0;
            int cur_node = -1;
            bool cont = false;
            vector<bool> visited(n, 0);
            for (int node: myPair.se) {
                if (visited[node]) {continue;}
                visited[node] = true;
                circle.pb(node);
                cur_node = node;
                for (int next_node: node_to_friends[node]) {
                    if (visited[next_node]) {return 0;}
                    visited[next_node] = true;
                    answer[next_node][cur_node] = 1;
                    answer[cur_node][next_node] = 1;
                    cur_node = next_node;
                }
            }
        }
        if (to_push_end != -1) {
            circle.insert(circle.begin(), to_push_start);
            circle.pb(to_push_end);
        }
        
        if ((two_pure == 1 || two_pure == 2) && circle.size() <= 2) {
            return 0;
        }
        if ((three_pure == 0 || three_pure == 1 || three_pure == 2 || three_pure == 3 || three_pure == 4)) {
            return 0;
        }
        if (three_pure == circle.size()) {
            return 0;
        }
        int bonus;
        if (two_pure == - 1) {
            bonus = 1;
        }
        else {
            bonus = 0;
        }
        forn(j, circle.size()-1 + bonus) {
            if (circle[j] != circle[(j+1)%circle.size()]) {
                answer[circle[j]][circle[(j+1)%circle.size()]] = 1;
                answer[circle[(j+1)%circle.size()]][circle[j]] = 1;
            }
        }
    }


	build(answer);
	return 1;
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:134:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::set<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  134 |                     if (cnt == myPair.se.size()) {
      |                         ~~~~^~~~~~~~~~~~~~~~~~~
supertrees.cpp:143:17: warning: unused variable 'cnt' [-Wunused-variable]
  143 |             int cnt = 0;
      |                 ^~~
supertrees.cpp:145:18: warning: unused variable 'cont' [-Wunused-variable]
  145 |             bool cont = false;
      |                  ^~~~
supertrees.cpp:172:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  172 |         if (three_pure == circle.size()) {
      |             ~~~~~~~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 1 ms 288 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 18 ms 3072 KB Output is correct
7 Correct 593 ms 69112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 1 ms 288 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 18 ms 3072 KB Output is correct
7 Correct 593 ms 69112 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 0 ms 256 KB Output is correct
11 Correct 0 ms 324 KB Output is correct
12 Correct 11 ms 1152 KB Output is correct
13 Correct 251 ms 22136 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 5 ms 768 KB Output is correct
17 Correct 117 ms 12152 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 0 ms 256 KB Output is correct
20 Correct 64 ms 6136 KB Output is correct
21 Correct 277 ms 26872 KB Output is correct
22 Correct 251 ms 22264 KB Output is correct
23 Correct 420 ms 45560 KB Output is correct
24 Correct 253 ms 22136 KB Output is correct
25 Correct 126 ms 15352 KB Output is correct
26 Correct 115 ms 12280 KB Output is correct
27 Correct 418 ms 45560 KB Output is correct
28 Correct 251 ms 22136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
7 Correct 1 ms 256 KB Output is correct
8 Correct 12 ms 1152 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 256 KB Output is correct
12 Correct 11 ms 1152 KB Output is correct
13 Correct 256 ms 22264 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 5 ms 768 KB Output is correct
17 Correct 115 ms 12152 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
19 Correct 0 ms 256 KB Output is correct
20 Correct 0 ms 256 KB Output is correct
21 Correct 63 ms 5880 KB Output is correct
22 Correct 251 ms 22136 KB Output is correct
23 Correct 256 ms 22136 KB Output is correct
24 Correct 262 ms 22136 KB Output is correct
25 Correct 114 ms 12152 KB Output is correct
26 Correct 110 ms 12152 KB Output is correct
27 Correct 253 ms 22136 KB Output is correct
28 Correct 264 ms 22264 KB Output is correct
29 Correct 131 ms 12152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 65 ms 6180 KB Output is correct
5 Correct 286 ms 26872 KB Output is correct
6 Correct 253 ms 22264 KB Output is correct
7 Correct 425 ms 45780 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Correct 64 ms 5756 KB Output is correct
10 Correct 254 ms 22136 KB Output is correct
11 Correct 250 ms 22136 KB Output is correct
12 Correct 260 ms 22136 KB Output is correct
13 Correct 0 ms 256 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 64 ms 5880 KB Output is correct
17 Correct 254 ms 22136 KB Output is correct
18 Correct 254 ms 22392 KB Output is correct
19 Correct 253 ms 22648 KB Output is correct
20 Correct 257 ms 22100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 1 ms 288 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 18 ms 3072 KB Output is correct
7 Correct 593 ms 69112 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 0 ms 256 KB Output is correct
11 Correct 0 ms 324 KB Output is correct
12 Correct 11 ms 1152 KB Output is correct
13 Correct 251 ms 22136 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 5 ms 768 KB Output is correct
17 Correct 117 ms 12152 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 0 ms 256 KB Output is correct
20 Correct 64 ms 6136 KB Output is correct
21 Correct 277 ms 26872 KB Output is correct
22 Correct 251 ms 22264 KB Output is correct
23 Correct 420 ms 45560 KB Output is correct
24 Correct 253 ms 22136 KB Output is correct
25 Correct 126 ms 15352 KB Output is correct
26 Correct 115 ms 12280 KB Output is correct
27 Correct 418 ms 45560 KB Output is correct
28 Correct 251 ms 22136 KB Output is correct
29 Correct 1 ms 256 KB Output is correct
30 Correct 0 ms 256 KB Output is correct
31 Correct 1 ms 256 KB Output is correct
32 Correct 0 ms 256 KB Output is correct
33 Correct 1 ms 256 KB Output is correct
34 Correct 1 ms 256 KB Output is correct
35 Correct 1 ms 256 KB Output is correct
36 Correct 12 ms 1152 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 256 KB Output is correct
40 Correct 11 ms 1152 KB Output is correct
41 Correct 256 ms 22264 KB Output is correct
42 Correct 0 ms 256 KB Output is correct
43 Correct 1 ms 256 KB Output is correct
44 Correct 5 ms 768 KB Output is correct
45 Correct 115 ms 12152 KB Output is correct
46 Correct 1 ms 256 KB Output is correct
47 Correct 0 ms 256 KB Output is correct
48 Correct 0 ms 256 KB Output is correct
49 Correct 63 ms 5880 KB Output is correct
50 Correct 251 ms 22136 KB Output is correct
51 Correct 256 ms 22136 KB Output is correct
52 Correct 262 ms 22136 KB Output is correct
53 Correct 114 ms 12152 KB Output is correct
54 Correct 110 ms 12152 KB Output is correct
55 Correct 253 ms 22136 KB Output is correct
56 Correct 264 ms 22264 KB Output is correct
57 Correct 131 ms 12152 KB Output is correct
58 Correct 0 ms 256 KB Output is correct
59 Correct 0 ms 256 KB Output is correct
60 Correct 5 ms 768 KB Output is correct
61 Correct 113 ms 12152 KB Output is correct
62 Correct 0 ms 256 KB Output is correct
63 Correct 0 ms 256 KB Output is correct
64 Correct 1 ms 256 KB Output is correct
65 Correct 65 ms 5880 KB Output is correct
66 Correct 112 ms 12152 KB Output is correct
67 Correct 109 ms 12116 KB Output is correct
68 Correct 109 ms 12280 KB Output is correct
69 Correct 110 ms 12280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 1 ms 288 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 18 ms 3072 KB Output is correct
7 Correct 593 ms 69112 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 0 ms 256 KB Output is correct
11 Correct 0 ms 324 KB Output is correct
12 Correct 11 ms 1152 KB Output is correct
13 Correct 251 ms 22136 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 5 ms 768 KB Output is correct
17 Correct 117 ms 12152 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 0 ms 256 KB Output is correct
20 Correct 64 ms 6136 KB Output is correct
21 Correct 277 ms 26872 KB Output is correct
22 Correct 251 ms 22264 KB Output is correct
23 Correct 420 ms 45560 KB Output is correct
24 Correct 253 ms 22136 KB Output is correct
25 Correct 126 ms 15352 KB Output is correct
26 Correct 115 ms 12280 KB Output is correct
27 Correct 418 ms 45560 KB Output is correct
28 Correct 251 ms 22136 KB Output is correct
29 Correct 1 ms 256 KB Output is correct
30 Correct 0 ms 256 KB Output is correct
31 Correct 1 ms 256 KB Output is correct
32 Correct 0 ms 256 KB Output is correct
33 Correct 1 ms 256 KB Output is correct
34 Correct 1 ms 256 KB Output is correct
35 Correct 1 ms 256 KB Output is correct
36 Correct 12 ms 1152 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 256 KB Output is correct
40 Correct 11 ms 1152 KB Output is correct
41 Correct 256 ms 22264 KB Output is correct
42 Correct 0 ms 256 KB Output is correct
43 Correct 1 ms 256 KB Output is correct
44 Correct 5 ms 768 KB Output is correct
45 Correct 115 ms 12152 KB Output is correct
46 Correct 1 ms 256 KB Output is correct
47 Correct 0 ms 256 KB Output is correct
48 Correct 0 ms 256 KB Output is correct
49 Correct 63 ms 5880 KB Output is correct
50 Correct 251 ms 22136 KB Output is correct
51 Correct 256 ms 22136 KB Output is correct
52 Correct 262 ms 22136 KB Output is correct
53 Correct 114 ms 12152 KB Output is correct
54 Correct 110 ms 12152 KB Output is correct
55 Correct 253 ms 22136 KB Output is correct
56 Correct 264 ms 22264 KB Output is correct
57 Correct 131 ms 12152 KB Output is correct
58 Correct 0 ms 256 KB Output is correct
59 Correct 0 ms 256 KB Output is correct
60 Correct 0 ms 256 KB Output is correct
61 Correct 65 ms 6180 KB Output is correct
62 Correct 286 ms 26872 KB Output is correct
63 Correct 253 ms 22264 KB Output is correct
64 Correct 425 ms 45780 KB Output is correct
65 Correct 1 ms 256 KB Output is correct
66 Correct 64 ms 5756 KB Output is correct
67 Correct 254 ms 22136 KB Output is correct
68 Correct 250 ms 22136 KB Output is correct
69 Correct 260 ms 22136 KB Output is correct
70 Correct 0 ms 256 KB Output is correct
71 Correct 1 ms 256 KB Output is correct
72 Correct 0 ms 256 KB Output is correct
73 Correct 64 ms 5880 KB Output is correct
74 Correct 254 ms 22136 KB Output is correct
75 Correct 254 ms 22392 KB Output is correct
76 Correct 253 ms 22648 KB Output is correct
77 Correct 257 ms 22100 KB Output is correct
78 Correct 0 ms 256 KB Output is correct
79 Correct 0 ms 256 KB Output is correct
80 Correct 5 ms 768 KB Output is correct
81 Correct 113 ms 12152 KB Output is correct
82 Correct 0 ms 256 KB Output is correct
83 Correct 0 ms 256 KB Output is correct
84 Correct 1 ms 256 KB Output is correct
85 Correct 65 ms 5880 KB Output is correct
86 Correct 112 ms 12152 KB Output is correct
87 Correct 109 ms 12116 KB Output is correct
88 Correct 109 ms 12280 KB Output is correct
89 Correct 110 ms 12280 KB Output is correct
90 Correct 0 ms 256 KB Output is correct
91 Correct 1 ms 256 KB Output is correct
92 Correct 5 ms 768 KB Output is correct
93 Correct 114 ms 12152 KB Output is correct
94 Correct 0 ms 256 KB Output is correct
95 Correct 0 ms 256 KB Output is correct
96 Correct 0 ms 256 KB Output is correct
97 Correct 28 ms 3320 KB Output is correct
98 Correct 111 ms 12152 KB Output is correct
99 Correct 111 ms 12152 KB Output is correct
100 Correct 108 ms 12280 KB Output is correct
101 Correct 111 ms 12152 KB Output is correct
102 Correct 116 ms 12372 KB Output is correct
103 Correct 122 ms 12280 KB Output is correct
104 Correct 347 ms 35960 KB Output is correct
105 Correct 118 ms 12152 KB Output is correct