Submission #993039
Source Code Expand
#include <bits/stdc++.h> using namespace std; typedef long long ll; class Union_Find { private: public: std::vector<int> par; std::vector<int> rank; Union_Find(int n) { // 初期化: O(n) par = rank = std::vector<int>(n); for(int i=0; i<n; i++) { par[i] = i; rank[0] = 0; } } int root(int x) { // 木の根を求める: O(α(n)) if(par[x] == x) return x; return par[x] = root(par[x]); // 親を辿りながら根に付け替える } void unite(int x, int y) { // x,yを含む集合を結合: O(α(n)) x = root(x); y = root(y); if( x == y ) return; if( rank[x] < rank[y] ) par[x] = y; else { par[y] = x; if( rank[x] == rank[y] ) rank[x]++; } } bool same(int x, int y) { //x,yが同じ集合に含まれるかどうか: O(α(n)) return root(x) == root(y); } }; int main () { int N,M; cin >> N >> M; // 言語 i を話せる人集合 : g[i] vector<vector<int>> g(M); for(int i=0; i<N; i++) { int K; cin >> K; for (int j=0; j<K; ++j) { int in; cin >> in; in--; // 0-index assert(0<=in and in<M); g[in].push_back(i); } } Union_Find uf(N); // 同じ言語が話せる人はマージ for(int i=0; i<M; i++) { for(int j=0; j<g[i].size(); j++) { uf.unite(g[i][0], g[i][j]); } } // 全員同じ集合ならYES bool ans = true; for(int i=0; i<N; i++) { cout << uf.par[i] << " " ; if( uf.par[0] != uf.par[i] ) { ans = false; } } if(ans) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
Submission Info
Submission Time | |
---|---|
Task | C - Interpretation |
User | takefekat |
Language | C++14 (GCC 5.4.1) |
Score | 0 |
Code Size | 1653 Byte |
Status | WA |
Exec Time | 59 ms |
Memory | 4736 KB |
Judge Result
Set Name | sample | dataset1 | dataset2 | ||||||
---|---|---|---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 0 / 200 | 0 / 200 | ||||||
Status |
|
|
|
Set Name | Test Cases |
---|---|
sample | sample-01.txt, sample-02.txt |
dataset1 | sample-01.txt, sample-02.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt |
dataset2 | sample-01.txt, sample-02.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 02-01.txt, 02-02.txt, 02-03.txt, 02-04.txt, 02-05.txt, 02-06.txt, 02-07.txt, 02-08.txt, 02-09.txt, 02-10.txt, 02-11.txt, 02-12.txt, 02-13.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
01-01.txt | WA | 2 ms | 256 KB |
01-02.txt | WA | 2 ms | 256 KB |
01-03.txt | WA | 3 ms | 256 KB |
01-04.txt | WA | 2 ms | 256 KB |
01-05.txt | WA | 3 ms | 256 KB |
01-06.txt | WA | 3 ms | 256 KB |
01-07.txt | WA | 3 ms | 256 KB |
01-08.txt | WA | 3 ms | 256 KB |
01-09.txt | WA | 3 ms | 256 KB |
01-10.txt | WA | 3 ms | 256 KB |
02-01.txt | WA | 43 ms | 4608 KB |
02-02.txt | WA | 50 ms | 2048 KB |
02-03.txt | WA | 42 ms | 2944 KB |
02-04.txt | WA | 57 ms | 4736 KB |
02-05.txt | WA | 53 ms | 2304 KB |
02-06.txt | WA | 57 ms | 4736 KB |
02-07.txt | WA | 56 ms | 2432 KB |
02-08.txt | WA | 42 ms | 1764 KB |
02-09.txt | WA | 59 ms | 4088 KB |
02-10.txt | WA | 47 ms | 3708 KB |
02-11.txt | WA | 47 ms | 3708 KB |
02-12.txt | WA | 48 ms | 3584 KB |
02-13.txt | WA | 48 ms | 3584 KB |
sample-01.txt | WA | 2 ms | 256 KB |
sample-02.txt | WA | 2 ms | 256 KB |