QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#638016#9449. New School Termucup-team4992WA 0ms3848kbC++203.0kb2024-10-13 14:39:312024-10-13 14:39:34

Judging History

你现在查看的是最新测评结果

  • [2024-10-13 14:39:34]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3848kb
  • [2024-10-13 14:39:31]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;

const int MAXN = 5000+5;
int n, m, nn;
int par[MAXN*4], sz1[MAXN*4], sz2[MAXN*4];
int vis[MAXN*4], ans[MAXN*4];
vector<pii> ps;

int trans(int x){
    if(x > nn){
        return x - nn;
    }
    return x + nn;
}

int find(int x){
    return par[x] == x ? par[x] : par[x] = find(par[x]);
}

void unite(int x, int y){
    x = find(x);
    y = find(y);
    if(x > y) swap(x, y);
    par[y] = x;
    sz1[x] += sz1[y];
    sz2[x] += sz2[y];
}

int check(vector<pii> vec){
    int A = 0, B = 0;
    for(pii p : vec){
        int a = p.first, b = p.second;
        if(a > b) swap(a, b);
        if(A < B){
            A += b;
            B += a;
        }
        else{
            A += a;
            B += b;
        }
        if(A > n || B > n){
            return 0;
        }
    }
    return 1;
}

void solve(int a, int b){
    if(find(a) == find(b)) return;
    if(find(a) == find(trans(b))) return;
    int fa = find(a), fb = find(b);
    vector<pii> vec;
    for(int i = 1; i <= nn; i++){
        vis[i] = 0;
    }
    for(int i = 1; i <= nn; i++){
        find(i);
        find(trans(i));
        if(vis[par[i]] || vis[par[trans(i)]]) continue;
        if(par[i] == fa || par[i] == fb || par[trans(i)] == fa || par[trans(i)] == fb) continue;
        vec.push_back(pii(sz1[par[i]], sz2[par[i]]));
        vis[par[i]] = 1;
    }
    vec.push_back(pii(sz1[fa]+sz2[fb], sz2[fa]+sz1[fb]));
    // cout << "vec:\n";
    // for(auto p : vec){
    //     cout << p.first << " " << p.second << "\n";
    // }
    if(check(vec)){
        // cout << "unite1 " << a << " " << b << "\n";
        unite(a, trans(b));
        unite(b, trans(a));
    }
    else{
        // cout << "unite2 " << a << " " << b << "\n";
        unite(a, b);
        unite(trans(a), trans(b));
    }
}

int main(){
    cin >> n >> m;
    nn = n*2;
    for(int i = 0; i < m; i++){
        int a, b;
        cin >> a >> b;
        ps.push_back(pii(a, b));
    }
    for(int i = 1; i <= nn*2; i++){
        par[i] = i;
        if(i <= nn){
            sz1[i] = 1;
        }
        else{
            sz2[i] = 1;
        }
    }
    reverse(ps.begin(), ps.end());
    for(auto p : ps){
        solve(p.first, p.second);
    }
    for(int i = 1; i <= nn; i++){
        vis[i] = 0;
    }
    int A = 0, B = 0;
    for(int i = 1; i <= nn; i++){
        find(i);
        find(trans(i));
        if(vis[par[i]] || vis[par[trans(i)]]) continue;
        int rev = 0;
        pii p = pii(sz1[par[i]], sz2[par[i]]);
        if(p.first > p.second){
            rev = 1;
            swap(p.first, p.second);
        }
        if(A < B){
            B += p.first;
            A += p.second;
            ans[par[i]] = rev;
        }
        else{
            A += p.first;
            B += p.second;
            ans[par[i]] = rev^1;

        }
        vis[par[i]] = 1;
    }
    for(int i = 1; i <= nn; i++){
        cout << ans[par[i]];
    }
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3548kb

input:

2 4
1 3
2 4
1 4
1 2

output:

1010

result:

ok Output is valid. OK

Test #2:

score: 0
Accepted
time: 0ms
memory: 3560kb

input:

3 7
2 5
1 3
4 6
2 6
4 5
2 4
5 6

output:

110010

result:

ok Output is valid. OK

Test #3:

score: 0
Accepted
time: 0ms
memory: 3848kb

input:

1 0

output:

01

result:

ok Output is valid. OK

Test #4:

score: 0
Accepted
time: 0ms
memory: 3844kb

input:

1 1
1 2

output:

10

result:

ok Output is valid. OK

Test #5:

score: 0
Accepted
time: 0ms
memory: 3504kb

input:

2 3
2 4
3 4
1 2

output:

1001

result:

ok Output is valid. OK

Test #6:

score: 0
Accepted
time: 0ms
memory: 3596kb

input:

3 8
4 6
3 5
1 4
2 4
1 6
1 2
3 4
4 5

output:

101010

result:

ok Output is valid. OK

Test #7:

score: -100
Wrong Answer
time: 0ms
memory: 3844kb

input:

4 9
4 7
3 8
1 5
2 7
2 8
6 8
7 8
1 4
1 6

output:

11100001

result:

wrong answer The division is not minimized.