Submission #853577

# Submission time Handle Problem Language Result Execution time Memory
853577 2023-09-24T16:36:18 Z ntkphong Connecting Supertrees (IOI20_supertrees) C++14
0 / 100
0 ms 600 KB
#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;

vector<int> lab;

int get_root(int u) {
    return lab[u] < 0 ? u : lab[u] = get_root(lab[u]);
}

bool unite(int u, int v) {
    u = get_root(u);
    v = get_root(v);
    
    if(u == v) return false;
    
    if(lab[u] > lab[v]) swap(u, v);
    
    lab[u] += lab[v];
    lab[v] = u;
    
    return true;
}

int construct(vector<vector<int>> p) {
	int n = p.size();
    
    lab.resize(n);
    for(int i = 0; i < n; i ++) lab[i] = -1;
    
    vector<vector<int>> answer(n, vector<int> (n, 0));	
    
	for (int i = 0; i < n; i ++) {
	    for(int j = 0; j < n; j ++) {
	        if(i == j || p[i][j] != 1) continue ;
	        
	        answer[i][j] = answer[j][i] = 1;
            unite(i, j);
	    }
	}
	
	for(int i = 0; i < n; i ++) {
        if(get_root(i) != i) continue ;
        
        vector<int> row;
        
        for(int j = 0; j < n; j ++) {
            if(p[i][j] != 2) continue ;
            row.push_back(j);
        }
        
        if(row.size() == 0) continue ; 
        
        int root = i;
        int k = i;
        int cnt = 1;
        
        for(int j = 0; j < row.size(); j ++) {
            if(unite(row[j], k)) {
                answer[row[j]][k] = 1;
                answer[k][row[j]] = 1;
                k = row[j];
                cnt ++ ;
            }
        }
        
        answer[root][k] = 1;
        answer[k][root] = 1;
        
        if(cnt <= 2) return 0;
	}
	
	for(int i = 0; i < n; i ++) {
        if(get_root(i) != i) continue ;
        
        vector<int> row;
        
        for(int j = 0; j < n; j ++) {
            if(p[i][j] != 3) continue ;
            row.push_back(j);
        }
        
        if(row.size() == 0) continue ; 
        
        int root = i;
        int k = i, prev_k = 0;
        int cnt = 1;
        
        for(int j = 0; j < row.size(); j ++) {
            if(unite(row[j], k)) {
                answer[j][k] = 1;
                answer[k][j] = 1;
                
                prev_k = k;
                k = j;
                cnt ++ ;
            }
        }
        
        answer[root][k] = 1;
        answer[k][root] = 1;
        
        answer[root][prev_k] = 1;
        answer[prev_k][root] = 1;
        
        if(cnt <= 3) return 0;
	}
	
	build(answer);
	return 1;
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:58:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |         for(int j = 0; j < row.size(); j ++) {
      |                        ~~^~~~~~~~~~~~
supertrees.cpp:89:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |         for(int j = 0; j < row.size(); j ++) {
      |                        ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 432 KB Output is correct
2 Correct 0 ms 600 KB Output is correct
3 Incorrect 0 ms 348 KB Too many ways to get from 0 to 2, should be 1 found no less than 2
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 432 KB Output is correct
2 Correct 0 ms 600 KB Output is correct
3 Incorrect 0 ms 348 KB Too many ways to get from 0 to 2, should be 1 found no less than 2
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 436 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Incorrect 0 ms 348 KB Answer gives possible 0 while actual possible 1
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 0 ms 432 KB Answer gives possible 0 while actual possible 1
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 432 KB Output is correct
2 Correct 0 ms 600 KB Output is correct
3 Incorrect 0 ms 348 KB Too many ways to get from 0 to 2, should be 1 found no less than 2
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 432 KB Output is correct
2 Correct 0 ms 600 KB Output is correct
3 Incorrect 0 ms 348 KB Too many ways to get from 0 to 2, should be 1 found no less than 2
4 Halted 0 ms 0 KB -