답안 #306673

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
306673 2020-09-26T05:43:35 Z vivaan_gupta 슈퍼트리 잇기 (IOI20_supertrees) C++14
100 / 100
584 ms 22392 KB
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <ratio>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <climits>
#include "supertrees.h"
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define in insert
#define vll vector<ll>
#define endl "\n"
#define f first
#define s second
#define pll pair<int,int>
// #define int ll
using namespace std;

 


 
struct DSU
{
	int connected;
	vector<int> par, sz;

	void init(int n) 
	{
		par = sz = vector<int> (n + 1, 0);
		for(int i = 1; i <= n; i++)
			par[i] = i, sz[i] = 1;
		connected = n;
	}

	int getPar(int u)
	{
		while(u != par[u])
		{
			par[u] = par[par[u]];
			u = par[u];
		}
		return u;
	}

	int getSize(int u)
	{
		return sz[getPar(u)];
	}

	void unite(int u, int v)
	{
		int par1 = getPar(u), par2 = getPar(v);

		if(par1 == par2)
			return;

		connected--;

		if(sz[par1] > sz[par2])
			swap(par1, par2);

		sz[par2] += sz[par1];
		sz[par1] = 0;
		par[par1] = par[par2];
	}
};



int construct(std::vector<std::vector<int>> p) {
	DSU g,g2;
	int n = p.size();
	g.init(n-1);
	g2.init(n-1);
	std::vector<std::vector<int>> ans;
	for (int i = 0; i < n; i++) {
		std::vector<int> row;
		row.resize(n);
		ans.push_back(row);
	}

	
	for(int i =0;i<n;i++){
		for(int j =i+1;j<n;j++){
			if(p[i][j] == 3) return 0;
			if(p[i][j] >= 1){
				g.unite(i,j);
			}
			if(p[i][j] == 1){
				g2.unite(i,j);
			}
		}
	}
	map<int,int> col;
	vector<int> wow[n+1];
	int c = 1;
	for(int i =0;i<n;i++){
		int op = g.getPar(i);
		if(col[op] == 0){
			col[op] = c;
			c++;				
		}
		wow[col[op]].pb(i);
	}
	c--;
	for(int i =0;i<n;i++){
		for(int j =0;j<n;j++){
			ans[i][j] = 0;
		}
	}
	for(int i = 1;i<=c;i++){
		if(wow[i].size() == 1) continue;
		set<int> contains;
		bool ok = true;
		for(int j = 0;j<wow[i].size();j++){
			
			for(int k =j+1;k<wow[i].size();k++){
				contains.in(p[wow[i][j]][wow[i][k]]);
				if(p[wow[i][j]][wow[i][k]] == 0) return 0;
				for(int l =0;l<wow[i].size();l++){
					// if(l == k || l == j) continue;
					if(p[wow[i][j]][wow[i][k]] == 2 && (p[wow[i][j]][wow[i][l]] == 1 && p[wow[i][l]][wow[i][k]] == 1))
						return 0;
				}
			}
		}
		if(contains.size() == 1 && *(contains.begin()) == 2){
			if(wow[i].size() == 2) return 0;
		}
		col.clear();
		int gg = 1;
		vector<int> chains[n+2];
		for(int j = 0;j<wow[i].size();j++){
			int op = g2.getPar(wow[i][j]);
			if(col[op] == 0){
				col[op] = gg;
				gg++;
			}
			chains[col[op]].pb(wow[i][j]);
		}
		vector<int> v;
		for(int j =1;j<gg;j++){
			
			// if(!poss) return 0;
			for(int k=0;k<chains[j].size()-1;k++){
				ans[chains[j][k]][chains[j][k+1]] = 1;
				ans[chains[j][k+1]][chains[j][k]] = 1;
			}			
			v.pb(chains[j].back());
		}

		for(int i= 0;i<v.size()-1;i++){
			ans[v[i]][v[i+1]] = 1;
			ans[v[i+1]][v[i]] = 1;
		}
		if(v.size() == 1) continue;
		ans[v.back()][v[0]] = 1;
		ans[v[0]][v.back()] = 1;
	}



	

	build(ans);
	return 1;
}


// void solve(){
	
// }
// signed main(){
 
//     ios_base::sync_with_stdio(0);
//     cin.tie(NULL);
//      // freopen(".in","r",stdin);freopen(".out","w",stdout);
    
//      ll tt=1;   
//      // cin >> tt;
//     while(tt--){    
//         solve();
//     }    
// }

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:142:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  142 |   for(int j = 0;j<wow[i].size();j++){
      |                 ~^~~~~~~~~~~~~~
supertrees.cpp:144:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  144 |    for(int k =j+1;k<wow[i].size();k++){
      |                   ~^~~~~~~~~~~~~~
supertrees.cpp:147:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  147 |     for(int l =0;l<wow[i].size();l++){
      |                  ~^~~~~~~~~~~~~~
supertrees.cpp:160:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  160 |   for(int j = 0;j<wow[i].size();j++){
      |                 ~^~~~~~~~~~~~~~
supertrees.cpp:172:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  172 |    for(int k=0;k<chains[j].size()-1;k++){
      |                ~^~~~~~~~~~~~~~~~~~~
supertrees.cpp:179:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  179 |   for(int i= 0;i<v.size()-1;i++){
      |                ~^~~~~~~~~~~
supertrees.cpp:141:8: warning: unused variable 'ok' [-Wunused-variable]
  141 |   bool ok = true;
      |        ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 288 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 13 ms 1280 KB Output is correct
7 Correct 500 ms 22136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 288 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 13 ms 1280 KB Output is correct
7 Correct 500 ms 22136 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 1 ms 256 KB Output is correct
12 Correct 10 ms 1280 KB Output is correct
13 Correct 232 ms 22236 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 110 ms 12248 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 0 ms 256 KB Output is correct
20 Correct 58 ms 5880 KB Output is correct
21 Correct 242 ms 22136 KB Output is correct
22 Correct 231 ms 22136 KB Output is correct
23 Correct 316 ms 22136 KB Output is correct
24 Correct 230 ms 22264 KB Output is correct
25 Correct 107 ms 12280 KB Output is correct
26 Correct 95 ms 12248 KB Output is correct
27 Correct 308 ms 22136 KB Output is correct
28 Correct 233 ms 22264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 1 ms 416 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 0 ms 256 KB Output is correct
7 Correct 1 ms 256 KB Output is correct
8 Correct 10 ms 1280 KB Output is correct
9 Correct 236 ms 22136 KB Output is correct
10 Correct 0 ms 256 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 13 ms 1280 KB Output is correct
13 Correct 584 ms 22264 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 896 KB Output is correct
17 Correct 105 ms 12280 KB Output is correct
18 Correct 0 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 60 ms 5880 KB Output is correct
22 Correct 245 ms 22136 KB Output is correct
23 Correct 239 ms 22236 KB Output is correct
24 Correct 344 ms 22136 KB Output is correct
25 Correct 95 ms 12280 KB Output is correct
26 Correct 99 ms 12280 KB Output is correct
27 Correct 234 ms 22392 KB Output is correct
28 Correct 335 ms 22264 KB Output is correct
29 Correct 95 ms 12372 KB Output is correct
# 결과 실행 시간 메모리 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 60 ms 5880 KB Output is correct
5 Correct 236 ms 22136 KB Output is correct
6 Correct 239 ms 22392 KB Output is correct
7 Correct 310 ms 22264 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Correct 60 ms 5880 KB Output is correct
10 Correct 236 ms 22136 KB Output is correct
11 Correct 235 ms 22264 KB Output is correct
12 Correct 330 ms 22236 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 58 ms 5884 KB Output is correct
17 Correct 242 ms 22392 KB Output is correct
18 Correct 246 ms 22136 KB Output is correct
19 Correct 233 ms 22392 KB Output is correct
20 Correct 232 ms 22392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 288 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 13 ms 1280 KB Output is correct
7 Correct 500 ms 22136 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 1 ms 256 KB Output is correct
12 Correct 10 ms 1280 KB Output is correct
13 Correct 232 ms 22236 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 110 ms 12248 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 0 ms 256 KB Output is correct
20 Correct 58 ms 5880 KB Output is correct
21 Correct 242 ms 22136 KB Output is correct
22 Correct 231 ms 22136 KB Output is correct
23 Correct 316 ms 22136 KB Output is correct
24 Correct 230 ms 22264 KB Output is correct
25 Correct 107 ms 12280 KB Output is correct
26 Correct 95 ms 12248 KB Output is correct
27 Correct 308 ms 22136 KB Output is correct
28 Correct 233 ms 22264 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 416 KB Output is correct
32 Correct 0 ms 256 KB Output is correct
33 Correct 0 ms 256 KB Output is correct
34 Correct 0 ms 256 KB Output is correct
35 Correct 1 ms 256 KB Output is correct
36 Correct 10 ms 1280 KB Output is correct
37 Correct 236 ms 22136 KB Output is correct
38 Correct 0 ms 256 KB Output is correct
39 Correct 0 ms 256 KB Output is correct
40 Correct 13 ms 1280 KB Output is correct
41 Correct 584 ms 22264 KB Output is correct
42 Correct 1 ms 256 KB Output is correct
43 Correct 0 ms 256 KB Output is correct
44 Correct 5 ms 896 KB Output is correct
45 Correct 105 ms 12280 KB Output is correct
46 Correct 0 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 60 ms 5880 KB Output is correct
50 Correct 245 ms 22136 KB Output is correct
51 Correct 239 ms 22236 KB Output is correct
52 Correct 344 ms 22136 KB Output is correct
53 Correct 95 ms 12280 KB Output is correct
54 Correct 99 ms 12280 KB Output is correct
55 Correct 234 ms 22392 KB Output is correct
56 Correct 335 ms 22264 KB Output is correct
57 Correct 95 ms 12372 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 105 ms 12244 KB Output is correct
62 Correct 1 ms 256 KB Output is correct
63 Correct 0 ms 256 KB Output is correct
64 Correct 0 ms 256 KB Output is correct
65 Correct 59 ms 5836 KB Output is correct
66 Correct 96 ms 12280 KB Output is correct
67 Correct 102 ms 12280 KB Output is correct
68 Correct 107 ms 12152 KB Output is correct
69 Correct 94 ms 12280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 288 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 13 ms 1280 KB Output is correct
7 Correct 500 ms 22136 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 1 ms 256 KB Output is correct
12 Correct 10 ms 1280 KB Output is correct
13 Correct 232 ms 22236 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 110 ms 12248 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 0 ms 256 KB Output is correct
20 Correct 58 ms 5880 KB Output is correct
21 Correct 242 ms 22136 KB Output is correct
22 Correct 231 ms 22136 KB Output is correct
23 Correct 316 ms 22136 KB Output is correct
24 Correct 230 ms 22264 KB Output is correct
25 Correct 107 ms 12280 KB Output is correct
26 Correct 95 ms 12248 KB Output is correct
27 Correct 308 ms 22136 KB Output is correct
28 Correct 233 ms 22264 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 416 KB Output is correct
32 Correct 0 ms 256 KB Output is correct
33 Correct 0 ms 256 KB Output is correct
34 Correct 0 ms 256 KB Output is correct
35 Correct 1 ms 256 KB Output is correct
36 Correct 10 ms 1280 KB Output is correct
37 Correct 236 ms 22136 KB Output is correct
38 Correct 0 ms 256 KB Output is correct
39 Correct 0 ms 256 KB Output is correct
40 Correct 13 ms 1280 KB Output is correct
41 Correct 584 ms 22264 KB Output is correct
42 Correct 1 ms 256 KB Output is correct
43 Correct 0 ms 256 KB Output is correct
44 Correct 5 ms 896 KB Output is correct
45 Correct 105 ms 12280 KB Output is correct
46 Correct 0 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 60 ms 5880 KB Output is correct
50 Correct 245 ms 22136 KB Output is correct
51 Correct 239 ms 22236 KB Output is correct
52 Correct 344 ms 22136 KB Output is correct
53 Correct 95 ms 12280 KB Output is correct
54 Correct 99 ms 12280 KB Output is correct
55 Correct 234 ms 22392 KB Output is correct
56 Correct 335 ms 22264 KB Output is correct
57 Correct 95 ms 12372 KB Output is correct
58 Correct 1 ms 256 KB Output is correct
59 Correct 0 ms 256 KB Output is correct
60 Correct 1 ms 256 KB Output is correct
61 Correct 60 ms 5880 KB Output is correct
62 Correct 236 ms 22136 KB Output is correct
63 Correct 239 ms 22392 KB Output is correct
64 Correct 310 ms 22264 KB Output is correct
65 Correct 1 ms 256 KB Output is correct
66 Correct 60 ms 5880 KB Output is correct
67 Correct 236 ms 22136 KB Output is correct
68 Correct 235 ms 22264 KB Output is correct
69 Correct 330 ms 22236 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 58 ms 5884 KB Output is correct
74 Correct 242 ms 22392 KB Output is correct
75 Correct 246 ms 22136 KB Output is correct
76 Correct 233 ms 22392 KB Output is correct
77 Correct 232 ms 22392 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 105 ms 12244 KB Output is correct
82 Correct 1 ms 256 KB Output is correct
83 Correct 0 ms 256 KB Output is correct
84 Correct 0 ms 256 KB Output is correct
85 Correct 59 ms 5836 KB Output is correct
86 Correct 96 ms 12280 KB Output is correct
87 Correct 102 ms 12280 KB Output is correct
88 Correct 107 ms 12152 KB Output is correct
89 Correct 94 ms 12280 KB Output is correct
90 Correct 0 ms 256 KB Output is correct
91 Correct 0 ms 256 KB Output is correct
92 Correct 4 ms 768 KB Output is correct
93 Correct 99 ms 12280 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 26 ms 3320 KB Output is correct
98 Correct 100 ms 12152 KB Output is correct
99 Correct 95 ms 12152 KB Output is correct
100 Correct 93 ms 12152 KB Output is correct
101 Correct 94 ms 12152 KB Output is correct
102 Correct 98 ms 12152 KB Output is correct
103 Correct 100 ms 12280 KB Output is correct
104 Correct 97 ms 12152 KB Output is correct
105 Correct 102 ms 12152 KB Output is correct