結果
| 問題 |
No.1660 Matrix Exponentiation
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-08-27 23:18:52 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,464 bytes |
| コンパイル時間 | 2,202 ms |
| コンパイル使用メモリ | 197,236 KB |
| 最終ジャッジ日時 | 2025-01-24 03:39:46 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 11 WA * 16 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
struct UnionFind{
vector<int> par;
vector<int> rank;
UnionFind(int n){
par.resize(n);
rank.resize(n);
for(int i = 0; i < n; i++){
par[i] = i;
rank[i] = 1;
}
}
int root(int x){
if(par[x] == x) return x;
else return par[x] = root(par[x]);
}
bool same(int x, int y){
return root(x) == root(y);
}
void unite(int x, int y){
x = root(x);
y = root(y);
if(x == y) return;
if(rank[x] < rank[y]) swap(x, y);
par[y] = x;
rank[x] += rank[y];
}
int size(int x) {
return rank[root(x)];
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, K;
cin >> N >> K;
UnionFind U(2 * N);
vector<int> r(K), c(K);
bool f = false;
for(int i = 0; i < K; i++) {
cin >> r[i] >> c[i];
r[i]--;
c[i]--;
if(U.same(r[i], N + c[i])){
f = true;
}
U.unite(r[i], N + c[i]);
}
if(f){
cout << -1 << endl;
return 0;
}
if(K == 0){
cout << 1 << endl;
return 0;
}
for(int i = 0; i < K; i++){
if(r[i] == c[i]){
cout << -1 << endl;
return 0;
}
}
int ma = 0;
for(int i = 0; i < N; i++){
ma = max(U.size(i), ma);
}
cout << ma / 2 + 1 << endl;
}