提出 #74552879
ソースコード 拡げる
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define vec vector
#define pll pair<ll,ll>
const ll N = 2e5 + 10;
const ll mod = 1e9 + 7;
ll far[N * 2],rankk[N * 2],vis[N * 2];
vec<ll> g[N];
bool ok = true;
void init(ll n){
for(int i = 1 ; i <= 2 * n ; i++){
far[i] = i;
rankk[i] = (i <= n ? 1 : 0);
vis[i] = 0;
}
}
ll find(ll x){
return far[x] == x ? x : far[x] = find(far[x]);
}
void solve(){
ll n,q;cin >> n >> q;
init(n);
ll ans = 0;
while(q--){
ll u,v;cin >> u >> v;
ll x = find(u);
ll y = find(v);
ll x1 = find(u + n);
ll y1 = find(v + n);
if(x == y || !ok){//不符合两个端点的染色不一样
cout << "-1\n";
ok = false;
}else if(x == y1){//两个点已经在一个集合里面
cout << ans << "\n";
}else{
if(vis[x]) ans -= rankk[x],vis[x] = false;
if(vis[y]) ans -= rankk[y],vis[y] = false;
if(vis[x1]) ans -= rankk[x1],vis[x1] = false;
if(vis[y1]) ans -= rankk[y1],vis[y1] = false;
//清空合并之前的贡献,加新的贡献,以免贡献重复
rankk[x] += rankk[y1];
rankk[y] += rankk[x1];
far[y1] = x;
far[x1] = y;
if(rankk[x] > rankk[y]){
ans += rankk[y];
vis[y]++;
}else{
ans += rankk[x];
vis[x]++;
}
cout << ans << "\n";
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
int T = 1;
// cin >> T;
while(T--){
solve();
}
return 0;
}
提出情報
| 提出日時 | |
|---|---|
| 問題 | F - Make Bipartite 3 |
| ユーザ | Xiao_Zhang |
| 言語 | C++23 (GCC 15.2.0) |
| 得点 | 500 |
| コード長 | 1496 Byte |
| 結果 | AC |
| 実行時間 | 54 ms |
| メモリ | 14628 KiB |
ジャッジ結果
| セット名 | Sample | All | ||||
|---|---|---|---|---|---|---|
| 得点 / 配点 | 0 / 0 | 500 / 500 | ||||
| 結果 |
|
|
| セット名 | テストケース |
|---|---|
| Sample | 00_sample_00.txt, 00_sample_01.txt |
| All | 00_sample_00.txt, 00_sample_01.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 02_corner_00.txt, 02_corner_01.txt, 02_corner_02.txt, 02_corner_03.txt, 02_corner_04.txt |
| ケース名 | 結果 | 実行時間 | メモリ |
|---|---|---|---|
| 00_sample_00.txt | AC | 2 ms | 3688 KiB |
| 00_sample_01.txt | AC | 2 ms | 3544 KiB |
| 01_random_00.txt | AC | 41 ms | 12976 KiB |
| 01_random_01.txt | AC | 43 ms | 13032 KiB |
| 01_random_02.txt | AC | 31 ms | 13072 KiB |
| 01_random_03.txt | AC | 33 ms | 13164 KiB |
| 01_random_04.txt | AC | 39 ms | 13096 KiB |
| 01_random_05.txt | AC | 42 ms | 13220 KiB |
| 01_random_06.txt | AC | 46 ms | 12960 KiB |
| 01_random_07.txt | AC | 45 ms | 13032 KiB |
| 01_random_08.txt | AC | 43 ms | 12976 KiB |
| 01_random_09.txt | AC | 44 ms | 13016 KiB |
| 01_random_10.txt | AC | 43 ms | 13092 KiB |
| 01_random_11.txt | AC | 44 ms | 13016 KiB |
| 01_random_12.txt | AC | 52 ms | 13032 KiB |
| 01_random_13.txt | AC | 53 ms | 13096 KiB |
| 01_random_14.txt | AC | 53 ms | 13072 KiB |
| 01_random_15.txt | AC | 54 ms | 13016 KiB |
| 01_random_16.txt | AC | 54 ms | 13028 KiB |
| 01_random_17.txt | AC | 53 ms | 13032 KiB |
| 02_corner_00.txt | AC | 42 ms | 14552 KiB |
| 02_corner_01.txt | AC | 41 ms | 14628 KiB |
| 02_corner_02.txt | AC | 45 ms | 13408 KiB |
| 02_corner_03.txt | AC | 42 ms | 13224 KiB |
| 02_corner_04.txt | AC | 2 ms | 3552 KiB |