Submission #419958

#TimeUsernameProblemLanguageResultExecution timeMemory
419958ioiConnecting Supertrees (IOI20_supertrees)C++14
96 / 100
336 ms27184 KiB
#include "supertrees.h" #include <vector> #include<bits/stdc++.h> using namespace std ; const int N = 1002; int dsu[N] , sz[N]; int get(int u){ if(u == dsu[u]) return u ; return dsu[u] = get(dsu[u]); } void connect(int u , int v){ u = get(u) , v = get(v); if(u == v)return ; if(sz[u] > sz[v])swap(u , v); sz[v] += sz[u]; dsu[u] = v ; } bool same(int u , int v){ u = get(u) , v = get(v); return u == v ; } vector<int> v , adj[N]; int construct(vector<vector<int>> p) { int n = p.size(); vector<vector<int>> answer (n , vector<int> (n , 0)); for(int i = 0 ; i < n ; i ++)sz[i] = 1 , dsu[i] = i ; int cnt[n] = {} , cnt1[n] = {}; for(int i = 0 ; i < n ; i ++){ for(int j = 0 ; j < n ; j ++){ if(p[i][j])connect(i , j); if(i == j)continue; cnt[i] += (p[i][j] == 2 );cnt1[i] += (p[i][j] == 1); if(p[i][j] == 1 )adj[i].push_back(j) ;// , adj[j].push_back(i); } } map<int, set<int> > mp ; for(int i = 0 ; i < n ; i ++){ for(int j = 0 ; j < n ;j ++) { // if(p[i][j] == 0&& same(i , j) || (p[i][j] && sz[get(dsu[i])] == 2))return 0 ; if(p[i][j] && i != j)mp[dsu[i]].insert(i) , mp[dsu[i]].insert(j); } } int vis[n] = {}; for(auto f : mp){ for(auto it : f.second){ for(auto it2 : f.second){ if(p[it][it2] == 0)return 0 ; } } // cout << "FF\n"; vector<int> cycle ; map<int , int > inc ; for(auto ff : f.second){ if(!vis[ff]){ vis[ff] ++ ; cycle.push_back(ff); inc[ff] ++ ; for(auto it : adj[ff]){ vis[it] ++ ; } } } if(cycle.size() == 1){ int last = cycle[0]; vector<int> v ; v.push_back(cycle[0]); for(auto it : adj[cycle[0]]){ // cout << "last " << last << " " << it << endl ; answer[it][last] = answer[last][it] = 1 ; last = it ; v.push_back(it); } for(int i = 0 ; i < v.size() ; i ++){ for(int j = i + 1 ; j < v.size() ; j ++){ if(p[v[i]][v[j]] != 1) return 0 ; } } } else { if(cycle.size() < 3)return 0 ; int last = cycle[0]; for(int i = 1 ; i < cycle.size() ; i ++){ int curr = cycle[i]; answer[last][curr] = answer[curr][last] = 1 ; last = curr ; } answer[cycle[0]][last] = answer[last][cycle[0]] = 1 ; for(auto ff : cycle){ int last = ff ; for(auto it : adj[ff]){ answer[it][last] = answer[last][it] = 1 ; last = it ; for(auto it2 : adj[it]){ if(inc.count(it2) && it2 != ff)return 0 ; } } } } } for(int i = 0 ; i < n ; i ++)answer[i][i] = 0 ; build(answer); return 1; }

Compilation message (stderr)

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:116:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  116 |                 for(int i = 0 ; i < v.size() ; i ++){
      |                                 ~~^~~~~~~~~~
supertrees.cpp:117:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  117 |                     for(int j = i + 1 ; j < v.size() ; j ++){
      |                                         ~~^~~~~~~~~~
supertrees.cpp:132:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  132 |                 for(int i = 1 ; i < cycle.size() ; i ++){
      |                                 ~~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...