Submission #735054

# Submission time Handle Problem Language Result Execution time Memory
735054 2023-05-03T12:43:20 Z vjudge1 Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
356 ms 22116 KB
#include "supertrees.h"
#include <bits/stdc++.h>
#define MAX 300001
#define INF LLONG_MAX
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ins insert
#define ff first
#define ss second
#define gett(x,m) get<m>(x)
#define all(a) a.begin(),a.end()
#define lb(a,b) lower_bound(all(a),b)
#define ub(a,b) upper_bound(all(a),b)
#define sortv(a) sort(all(a))
#define sorta(a,sz) sort(a,a+sz)
#define inputar(a,b){\
    for(int i=0;i<b;i++){\
        cin >> a[i];\
    }\
}
#define inputvec(a,b){\
    for(int i=0;i<b;i++){\
        ll num;\
        cin >> num;\
        a.pb(num);\
    }\
}
#define outputar(a,b){\
    for(int i=0;i<b;i++){\
        cout << a[i] << " ";\
    }\
    cout << "\n";\
}
#define outputvec(a){\
    for(auto x:a){\
        cout << x << " ";\
    }\
    cout << "\n";\
}
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef tuple<ll,ll,ll> tll;
typedef pair<ll,ll> pll;
typedef double db;
typedef long double ldb;
inline void USACO(string filename){
  freopen((filename+".in").c_str(),"r",stdin);
  freopen((filename+".out").c_str(),"w",stdout);
}
struct dsu{
    vector<ll> e;
    void init(int n){
        e.resize(n+1,-1);
    }
    int get(int x){
        if(e[x]<0){
            return x;
        }
        else{
            return e[x]=get(e[x]);
        }
    }
    int size(int x){
        return -e[get(x)];
    }
    bool same_set(int x,int y){
        return (get(x)==get(y));
    }
    bool unite(int x,int y){
        x=get(x);
        y=get(y);
        if(x==y){
            return false;
        }
        if(e[x]>e[y]){
            swap(x,y);
        }
        e[x]+=e[y];
        e[y]=x;
        return true;
    }
};
int construct(vector<vector<int>> p) {
	int n = p.size();
	dsu d;
	d.init(n);
	vector<vector<int>> answer;
	answer.resize(n,vector<int>(n,0));
	for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(p[i][j]==1){
                d.unite(i,j);
            }
            if(p[i][j]==3){
                return 0;
            }
        }
	}
	for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(d.same_set(i,j) && p[i][j]!=1){
                //cout << 1 << "\n";
                return 0;
            }
        }
	}
	dsu d2;
	dsu d3;
	d2.init(n);
	d3.init(n);
	for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(p[i][j]==2){
                d2.unite(d.get(i),d.get(j));
            }
            else if(p[i][j]==3){
                d3.unite(d.get(i),d.get(j));
            }
        }
	}
	for(int i=0;i<n;i++){
        if(d2.size(i)==2){
            //cout << 2 << "\n";
            return 0;
        }
	}
	for(int i=0;i<n;i++){
	    if(d3.size(i)==2 || d3.size(i)==3){
            //cout << 3 << "\n";
            return 0;
	    }
	}
	for(int i=0;i<n;i++){
	    for(int j=0;j<n;j++){
	        if(p[i][j]==0 && (d2.same_set(i,j) || d3.same_set(i,j))){
	            return 0;
	        }
            if(d2.same_set(i,j) && d3.same_set(i,j) && i!=j){
                //cout << 4 << "\n";
                return 0;
            }
	    }
	}
	set<ll> st;
	for(int i=0;i<n;i++){
        st.ins(i);
	}
	for(int i=0;i<n;i++){
	    if(st.find(i)==st.end()){
            continue;
	    }
	    vector<int> c;
	    for(auto x:st){
            if(d.same_set(i,x)){
                c.pb(x);
            }
	    }
	    for(int i=1;i<c.size();i++){
	        answer[c[i-1]][c[i]]=1;
	        answer[c[i]][c[i-1]]=1;
	    }
	    for(auto x:c){
            st.erase(x);
	    }
	}
	st.clear();
	for(int i=0;i<n;i++){
        st.ins(i);
	}
	for(int i=0;i<n;i++){
	    if(st.find(i)==st.end()){
            continue;
	    }
	    set<int> e;
	    vector<int> c;
	    for(auto x:st){
            if(d2.same_set(i,x)){
                e.ins(d.get(x));
            }
	    }
	    for(auto x:e){
            c.pb(x);
	    }
	    if(c.size()>1){
	        for(int i=1;i<c.size();i++){
                answer[c[i-1]][c[i]]=1;
                answer[c[i]][c[i-1]]=1;
            }
            answer[c[0]][c[c.size()-1]]=1;
            answer[c[c.size()-1]][c[0]]=1;
	    }
	    for(auto x:c){
            st.erase(x);
	    }
	}
	st.clear();
	for(int i=0;i<n;i++){
        st.ins(i);
	}
	for(int i=0;i<n;i++){
	    if(st.find(i)==st.end()){
            continue;
	    }
	    set<int> e;
	    vector<int> c;
	    for(auto x:st){
            if(d3.same_set(i,x)){
                e.ins(d.get(x));
            }
	    }
	    for(auto x:e){
            c.pb(x);
	    }
	    if(c.size()>1){
	        for(int i=1;i<c.size();i++){
                answer[c[i-1]][c[i]]=1;
                answer[c[i]][c[i-1]]=1;
            }
            answer[c[0]][c[c.size()-1]]=1;
            answer[c[c.size()-1]][c[0]]=1;
            answer[c[1]][c[c.size()-1]]=1;
            answer[c[c.size()-1]][c[1]]=1;
	    }
	    for(auto x:c){
            st.erase(x);
	    }
	}
	build(answer);
	return 1;
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:161:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  161 |      for(int i=1;i<c.size();i++){
      |                  ~^~~~~~~~~
supertrees.cpp:188:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  188 |          for(int i=1;i<c.size();i++){
      |                      ~^~~~~~~~~
supertrees.cpp:218:23: 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<c.size();i++){
      |                      ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 224 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 9 ms 1140 KB Output is correct
7 Correct 203 ms 22024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 224 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 9 ms 1140 KB Output is correct
7 Correct 203 ms 22024 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 10 ms 1100 KB Output is correct
13 Correct 196 ms 22076 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 5 ms 724 KB Output is correct
17 Correct 93 ms 12108 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 59 ms 5760 KB Output is correct
21 Correct 259 ms 22048 KB Output is correct
22 Correct 255 ms 22056 KB Output is correct
23 Correct 271 ms 22116 KB Output is correct
24 Correct 236 ms 22092 KB Output is correct
25 Correct 139 ms 12108 KB Output is correct
26 Correct 82 ms 12108 KB Output is correct
27 Correct 356 ms 22068 KB Output is correct
28 Correct 232 ms 22028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 280 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 10 ms 1108 KB Output is correct
9 Correct 288 ms 22068 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 10 ms 1108 KB Output is correct
13 Correct 258 ms 22032 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 5 ms 724 KB Output is correct
17 Correct 121 ms 12036 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 50 ms 5676 KB Output is correct
22 Correct 215 ms 21964 KB Output is correct
23 Correct 240 ms 22032 KB Output is correct
24 Correct 290 ms 21980 KB Output is correct
25 Correct 74 ms 12048 KB Output is correct
26 Correct 76 ms 12048 KB Output is correct
27 Correct 197 ms 22012 KB Output is correct
28 Correct 222 ms 22064 KB Output is correct
29 Correct 77 ms 12044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 56 ms 5764 KB Output is correct
5 Correct 212 ms 22048 KB Output is correct
6 Correct 233 ms 22020 KB Output is correct
7 Correct 230 ms 22096 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 58 ms 5764 KB Output is correct
10 Correct 200 ms 22016 KB Output is correct
11 Correct 207 ms 22060 KB Output is correct
12 Correct 214 ms 22060 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 53 ms 5712 KB Output is correct
17 Correct 197 ms 22028 KB Output is correct
18 Correct 207 ms 22092 KB Output is correct
19 Correct 209 ms 22020 KB Output is correct
20 Correct 192 ms 21976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 224 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 9 ms 1140 KB Output is correct
7 Correct 203 ms 22024 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 10 ms 1100 KB Output is correct
13 Correct 196 ms 22076 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 5 ms 724 KB Output is correct
17 Correct 93 ms 12108 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 59 ms 5760 KB Output is correct
21 Correct 259 ms 22048 KB Output is correct
22 Correct 255 ms 22056 KB Output is correct
23 Correct 271 ms 22116 KB Output is correct
24 Correct 236 ms 22092 KB Output is correct
25 Correct 139 ms 12108 KB Output is correct
26 Correct 82 ms 12108 KB Output is correct
27 Correct 356 ms 22068 KB Output is correct
28 Correct 232 ms 22028 KB Output is correct
29 Correct 2 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 ms 280 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 10 ms 1108 KB Output is correct
37 Correct 288 ms 22068 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 10 ms 1108 KB Output is correct
41 Correct 258 ms 22032 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 5 ms 724 KB Output is correct
45 Correct 121 ms 12036 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 50 ms 5676 KB Output is correct
50 Correct 215 ms 21964 KB Output is correct
51 Correct 240 ms 22032 KB Output is correct
52 Correct 290 ms 21980 KB Output is correct
53 Correct 74 ms 12048 KB Output is correct
54 Correct 76 ms 12048 KB Output is correct
55 Correct 197 ms 22012 KB Output is correct
56 Correct 222 ms 22064 KB Output is correct
57 Correct 77 ms 12044 KB Output is correct
58 Correct 0 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 4 ms 724 KB Output is correct
61 Correct 81 ms 12032 KB Output is correct
62 Correct 1 ms 212 KB Output is correct
63 Correct 0 ms 212 KB Output is correct
64 Correct 1 ms 212 KB Output is correct
65 Correct 52 ms 5692 KB Output is correct
66 Correct 100 ms 12036 KB Output is correct
67 Correct 72 ms 12032 KB Output is correct
68 Correct 84 ms 12036 KB Output is correct
69 Correct 79 ms 12048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 224 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 9 ms 1140 KB Output is correct
7 Correct 203 ms 22024 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 10 ms 1100 KB Output is correct
13 Correct 196 ms 22076 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 5 ms 724 KB Output is correct
17 Correct 93 ms 12108 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 59 ms 5760 KB Output is correct
21 Correct 259 ms 22048 KB Output is correct
22 Correct 255 ms 22056 KB Output is correct
23 Correct 271 ms 22116 KB Output is correct
24 Correct 236 ms 22092 KB Output is correct
25 Correct 139 ms 12108 KB Output is correct
26 Correct 82 ms 12108 KB Output is correct
27 Correct 356 ms 22068 KB Output is correct
28 Correct 232 ms 22028 KB Output is correct
29 Correct 2 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 ms 280 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 10 ms 1108 KB Output is correct
37 Correct 288 ms 22068 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 10 ms 1108 KB Output is correct
41 Correct 258 ms 22032 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 5 ms 724 KB Output is correct
45 Correct 121 ms 12036 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 50 ms 5676 KB Output is correct
50 Correct 215 ms 21964 KB Output is correct
51 Correct 240 ms 22032 KB Output is correct
52 Correct 290 ms 21980 KB Output is correct
53 Correct 74 ms 12048 KB Output is correct
54 Correct 76 ms 12048 KB Output is correct
55 Correct 197 ms 22012 KB Output is correct
56 Correct 222 ms 22064 KB Output is correct
57 Correct 77 ms 12044 KB Output is correct
58 Correct 0 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 0 ms 212 KB Output is correct
61 Correct 56 ms 5764 KB Output is correct
62 Correct 212 ms 22048 KB Output is correct
63 Correct 233 ms 22020 KB Output is correct
64 Correct 230 ms 22096 KB Output is correct
65 Correct 0 ms 212 KB Output is correct
66 Correct 58 ms 5764 KB Output is correct
67 Correct 200 ms 22016 KB Output is correct
68 Correct 207 ms 22060 KB Output is correct
69 Correct 214 ms 22060 KB Output is correct
70 Correct 1 ms 212 KB Output is correct
71 Correct 1 ms 212 KB Output is correct
72 Correct 1 ms 212 KB Output is correct
73 Correct 53 ms 5712 KB Output is correct
74 Correct 197 ms 22028 KB Output is correct
75 Correct 207 ms 22092 KB Output is correct
76 Correct 209 ms 22020 KB Output is correct
77 Correct 192 ms 21976 KB Output is correct
78 Correct 0 ms 212 KB Output is correct
79 Correct 0 ms 212 KB Output is correct
80 Correct 4 ms 724 KB Output is correct
81 Correct 81 ms 12032 KB Output is correct
82 Correct 1 ms 212 KB Output is correct
83 Correct 0 ms 212 KB Output is correct
84 Correct 1 ms 212 KB Output is correct
85 Correct 52 ms 5692 KB Output is correct
86 Correct 100 ms 12036 KB Output is correct
87 Correct 72 ms 12032 KB Output is correct
88 Correct 84 ms 12036 KB Output is correct
89 Correct 79 ms 12048 KB Output is correct
90 Correct 1 ms 212 KB Output is correct
91 Correct 0 ms 212 KB Output is correct
92 Correct 4 ms 724 KB Output is correct
93 Correct 82 ms 12032 KB Output is correct
94 Correct 1 ms 212 KB Output is correct
95 Correct 1 ms 212 KB Output is correct
96 Correct 0 ms 212 KB Output is correct
97 Correct 19 ms 3212 KB Output is correct
98 Correct 82 ms 12044 KB Output is correct
99 Correct 77 ms 12108 KB Output is correct
100 Correct 73 ms 12032 KB Output is correct
101 Correct 74 ms 12036 KB Output is correct
102 Correct 99 ms 12032 KB Output is correct
103 Correct 101 ms 14000 KB Output is correct
104 Correct 74 ms 14028 KB Output is correct
105 Correct 79 ms 14068 KB Output is correct