Submission #735051

# Submission time Handle Problem Language Result Execution time Memory
735051 2023-05-03T12:41:16 Z vjudge1 Connecting Supertrees (IOI20_supertrees) C++17
96 / 100
348 ms 24076 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);
            }
        }
	}
	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:158:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  158 |      for(int i=1;i<c.size();i++){
      |                  ~^~~~~~~~~
supertrees.cpp:185:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  185 |          for(int i=1;i<c.size();i++){
      |                      ~^~~~~~~~~
supertrees.cpp:215:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  215 |          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 1 ms 340 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 212 KB Output is correct
6 Correct 9 ms 1108 KB Output is correct
7 Correct 212 ms 22020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 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 212 KB Output is correct
6 Correct 9 ms 1108 KB Output is correct
7 Correct 212 ms 22020 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 9 ms 1108 KB Output is correct
13 Correct 227 ms 22024 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 4 ms 724 KB Output is correct
17 Correct 104 ms 12036 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 72 ms 6264 KB Output is correct
21 Correct 245 ms 24056 KB Output is correct
22 Correct 237 ms 24048 KB Output is correct
23 Correct 261 ms 23952 KB Output is correct
24 Correct 265 ms 24072 KB Output is correct
25 Correct 114 ms 14072 KB Output is correct
26 Correct 100 ms 14028 KB Output is correct
27 Correct 348 ms 23972 KB Output is correct
28 Correct 218 ms 24068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 13 ms 1256 KB Output is correct
9 Correct 257 ms 23968 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 9 ms 1236 KB Output is correct
13 Correct 210 ms 23972 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 304 KB Output is correct
16 Correct 5 ms 852 KB Output is correct
17 Correct 108 ms 14060 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 296 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 54 ms 6264 KB Output is correct
22 Correct 205 ms 24000 KB Output is correct
23 Correct 241 ms 23940 KB Output is correct
24 Correct 250 ms 23980 KB Output is correct
25 Correct 93 ms 14088 KB Output is correct
26 Correct 100 ms 14072 KB Output is correct
27 Correct 239 ms 23924 KB Output is correct
28 Correct 277 ms 23972 KB Output is correct
29 Correct 86 ms 14076 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 1 ms 212 KB Output is correct
4 Correct 59 ms 6264 KB Output is correct
5 Correct 217 ms 24048 KB Output is correct
6 Correct 218 ms 24056 KB Output is correct
7 Correct 258 ms 24076 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 54 ms 6256 KB Output is correct
10 Correct 222 ms 23988 KB Output is correct
11 Correct 215 ms 23936 KB Output is correct
12 Correct 233 ms 23984 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 61 ms 6228 KB Output is correct
17 Correct 217 ms 23924 KB Output is correct
18 Correct 236 ms 24012 KB Output is correct
19 Correct 214 ms 23956 KB Output is correct
20 Correct 270 ms 24052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 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 212 KB Output is correct
6 Correct 9 ms 1108 KB Output is correct
7 Correct 212 ms 22020 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 9 ms 1108 KB Output is correct
13 Correct 227 ms 22024 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 4 ms 724 KB Output is correct
17 Correct 104 ms 12036 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 72 ms 6264 KB Output is correct
21 Correct 245 ms 24056 KB Output is correct
22 Correct 237 ms 24048 KB Output is correct
23 Correct 261 ms 23952 KB Output is correct
24 Correct 265 ms 24072 KB Output is correct
25 Correct 114 ms 14072 KB Output is correct
26 Correct 100 ms 14028 KB Output is correct
27 Correct 348 ms 23972 KB Output is correct
28 Correct 218 ms 24068 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 13 ms 1256 KB Output is correct
37 Correct 257 ms 23968 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 9 ms 1236 KB Output is correct
41 Correct 210 ms 23972 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 304 KB Output is correct
44 Correct 5 ms 852 KB Output is correct
45 Correct 108 ms 14060 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 1 ms 296 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 54 ms 6264 KB Output is correct
50 Correct 205 ms 24000 KB Output is correct
51 Correct 241 ms 23940 KB Output is correct
52 Correct 250 ms 23980 KB Output is correct
53 Correct 93 ms 14088 KB Output is correct
54 Correct 100 ms 14072 KB Output is correct
55 Correct 239 ms 23924 KB Output is correct
56 Correct 277 ms 23972 KB Output is correct
57 Correct 86 ms 14076 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 6 ms 816 KB Output is correct
61 Correct 105 ms 13992 KB Output is correct
62 Correct 1 ms 212 KB Output is correct
63 Correct 1 ms 212 KB Output is correct
64 Correct 1 ms 212 KB Output is correct
65 Correct 57 ms 6372 KB Output is correct
66 Correct 95 ms 14076 KB Output is correct
67 Correct 117 ms 14072 KB Output is correct
68 Correct 134 ms 14068 KB Output is correct
69 Correct 101 ms 14068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 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 212 KB Output is correct
6 Correct 9 ms 1108 KB Output is correct
7 Correct 212 ms 22020 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 9 ms 1108 KB Output is correct
13 Correct 227 ms 22024 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 4 ms 724 KB Output is correct
17 Correct 104 ms 12036 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 72 ms 6264 KB Output is correct
21 Correct 245 ms 24056 KB Output is correct
22 Correct 237 ms 24048 KB Output is correct
23 Correct 261 ms 23952 KB Output is correct
24 Correct 265 ms 24072 KB Output is correct
25 Correct 114 ms 14072 KB Output is correct
26 Correct 100 ms 14028 KB Output is correct
27 Correct 348 ms 23972 KB Output is correct
28 Correct 218 ms 24068 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 13 ms 1256 KB Output is correct
37 Correct 257 ms 23968 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 9 ms 1236 KB Output is correct
41 Correct 210 ms 23972 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 304 KB Output is correct
44 Correct 5 ms 852 KB Output is correct
45 Correct 108 ms 14060 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 1 ms 296 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 54 ms 6264 KB Output is correct
50 Correct 205 ms 24000 KB Output is correct
51 Correct 241 ms 23940 KB Output is correct
52 Correct 250 ms 23980 KB Output is correct
53 Correct 93 ms 14088 KB Output is correct
54 Correct 100 ms 14072 KB Output is correct
55 Correct 239 ms 23924 KB Output is correct
56 Correct 277 ms 23972 KB Output is correct
57 Correct 86 ms 14076 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 59 ms 6264 KB Output is correct
62 Correct 217 ms 24048 KB Output is correct
63 Correct 218 ms 24056 KB Output is correct
64 Correct 258 ms 24076 KB Output is correct
65 Correct 1 ms 212 KB Output is correct
66 Correct 54 ms 6256 KB Output is correct
67 Correct 222 ms 23988 KB Output is correct
68 Correct 215 ms 23936 KB Output is correct
69 Correct 233 ms 23984 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 61 ms 6228 KB Output is correct
74 Correct 217 ms 23924 KB Output is correct
75 Correct 236 ms 24012 KB Output is correct
76 Correct 214 ms 23956 KB Output is correct
77 Correct 270 ms 24052 KB Output is correct
78 Correct 1 ms 212 KB Output is correct
79 Correct 1 ms 212 KB Output is correct
80 Correct 6 ms 816 KB Output is correct
81 Correct 105 ms 13992 KB Output is correct
82 Correct 1 ms 212 KB Output is correct
83 Correct 1 ms 212 KB Output is correct
84 Correct 1 ms 212 KB Output is correct
85 Correct 57 ms 6372 KB Output is correct
86 Correct 95 ms 14076 KB Output is correct
87 Correct 117 ms 14072 KB Output is correct
88 Correct 134 ms 14068 KB Output is correct
89 Correct 101 ms 14068 KB Output is correct
90 Correct 1 ms 212 KB Output is correct
91 Correct 1 ms 212 KB Output is correct
92 Correct 5 ms 852 KB Output is correct
93 Correct 114 ms 14068 KB Output is correct
94 Correct 1 ms 212 KB Output is correct
95 Correct 1 ms 212 KB Output is correct
96 Correct 1 ms 212 KB Output is correct
97 Correct 24 ms 3704 KB Output is correct
98 Correct 108 ms 14068 KB Output is correct
99 Correct 105 ms 14064 KB Output is correct
100 Correct 97 ms 14068 KB Output is correct
101 Correct 96 ms 14080 KB Output is correct
102 Incorrect 216 ms 24048 KB Answer gives possible 1 while actual possible 0
103 Halted 0 ms 0 KB -