QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#410793#6705. Medianwzl19371WA 2ms3556kbC++142.3kb2024-05-14 15:01:522024-05-14 15:01:53

Judging History

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

  • [2024-05-14 15:01:53]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:3556kb
  • [2024-05-14 15:01:52]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
struct Edge{
    int to, nxt;
};
void solve(){
    int n,m; cin >> n >> m;
    int head[n+5]={}, ct=0, head2[n+5]={}, ct2=0;
    Edge a[m+5]={}, b[m+5]={};
    auto add = [&](int x, int y) {
        a[++ct] = {y, head[x]};
        b[++ct2] = {x, head2[y]};
        head[x] = ct;
        head2[y] = ct2;
    };
    int du[n+5] = {};
    // bool ok = true;
    for(int i=1;i<=m;i++) {
        int x, y; cin >> x >> y;
        add(x,y);
        // if(x == y) {
        //     ok = false;
        // }
        du[y]++;
    };
    // if(!ok) {
    //     cout << string('0', n) << endl;
    //     return;
    // }
    queue<int> q;
    for(int i=1;i<=n;i++) {
        if(du[i] == 0)
            q.push(i);
    }
    auto check_loop = [&]() -> bool {
        while(!q.empty()) {
            int x=q.front(); q.pop();
            for(int i=head[x];i;i=a[i].nxt) {
                du[a[i].to]--;
                if(du[a[i].to] == 0) {
                    q.push(a[i].to);
                }
            }
        }
        for(int i=1;i<=n;i++) {
            if(du[i] != 0)
                return true;
        }
        return false;
    };
    if(check_loop()) {
        cout << string(n, '0') << endl;
        return;
    }
    int gr[n+5]={}, ls[n+5]={};
    bool vis1[n+5]={}, vis2[n+5]={};
    function<int(int)> dfs = [&](int x) -> int {
        if(vis1[x]) return gr[x] + 1;
        vis1[x] = true;
        for(int i=head[x];i;i=a[i].nxt) {
            gr[x] += dfs(a[i].to);
        }
        return gr[x] + 1;
    };
    function<int(int)> dfs2 = [&](int x) -> int {
        if(vis2[x]) return ls[x] + 1;
        vis2[x] = true;
        for(int i=head2[x];i;i=b[i].nxt) {
            ls[x] += dfs2(b[i].to);
        }
        return ls[x] + 1;
    };
    for(int i=1;i<=n;i++) {
        if(!vis1[i])
            dfs(i);
        if(!vis2[i])
            dfs2(i);
    }
    for(int i=1;i<=n;i++) {
        if(gr[i] <= (n-1)/2 && ls[i] <= (n-1)/2)
            cout << '1';
        else
            cout << '0';
    }
    cout << endl;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t; cin >> t;
    while(t--)
        solve();
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3520kb

input:

2
5 4
1 2
3 2
2 4
2 5
3 2
1 1
2 3

output:

01000
000

result:

ok 2 lines

Test #2:

score: -100
Wrong Answer
time: 2ms
memory: 3556kb

input:

66
13 2
9 13
7 11
11 19
9 1
8 1
5 1
2 8
4 2
2 1
5 2
6 3
3 11
3 2
4 6
6 10
9 8
3 5
1 7
5 8
3 9
4 9
6 7
3 1
2 3
11 6
9 4
1 6
5 2
1 5
4 6
8 4
15 15
10 6
15 8
7 6
11 1
5 2
3 4
11 13
4 6
10 12
10 13
1 6
15 2
5 12
13 14
5 3
15 86
14 12
8 1
14 9
8 15
5 10
1 9
11 2
6 2
7 10
10 13
14 5
4 13
5 8
4 10
13 9
6 9...

output:

1111111111111
00000000111
111
11111111111
111111111111111
000000000000000
00000
01000
1111111
0000000000000
111000101
111111111
000000001010000
010111111
000000000
0000000000000
1111111111111
000000000000000
00000101001
000000000000000
11111111111
00000000000
11111
00000000000
11101110111
00000
1111...

result:

wrong answer 2nd lines differ - expected: '01001000111', found: '00000000111'