QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#261412#6705. MedianAH20AC ✓3ms18160kbC++142.0kb2023-11-22 21:01:532023-11-22 21:01:53

Judging History

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

  • [2023-11-22 21:01:53]
  • 评测
  • 测评结果:AC
  • 用时:3ms
  • 内存:18160kb
  • [2023-11-22 21:01:53]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;
const int maxm = 2e5+7;
vector<int>edge[maxm],rev[maxm];
int inv[maxm],ans[maxm],L[maxm],R[maxm],n,m;
int tinv[maxm],trev[maxm],tmp[maxm];
void init(){
    for(int i=1;i<=n;i++){
        edge[i].clear();
        rev[i].clear();
        inv[i] = ans[i] = L[i] = R[i] = 0;
        tinv[i] = trev[i] = 0;
        tmp[i] = 0;
    }
}
void topo(){
    //跑一遍拓扑,看看是不是DAG
    queue<int>Q;
    for(int i=1;i<=n;i++){
        if(inv[i] == 0) Q.push(i);
    }
    while(Q.size()){
        int now = Q.front();
        Q.pop();
        for(auto u:edge[now]){
            --inv[u];
            if(inv[u] == 0){
                Q.push(u);
            }
        }
    }
}
void print(){
    for(int i=1;i<=n;i++) cout<<ans[i];
    cout<<"\n";
}
int dfs1(int now){
    if(tmp[now]) return 0;
    tmp[now] = 1;
    for(auto u:edge[now]){
        tmp[now] += dfs1(u);
    }
    return tmp[now];
}
int dfs2(int now){
    if(tmp[now]) return 0;
    tmp[now] = 1;
    for(auto u:rev[now]){
        tmp[now] += dfs2(u);
    }
    return tmp[now];
}
void calc1(){
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++) tmp[j] = 0;
        dfs1(i);
        L[i] = tmp[i];
    }
}
void calc2(){
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++) tmp[j] = 0;
        dfs2(i);
        R[i] = tmp[i];
    }
}
void solve(){
    cin>>n>>m;
    init();
    for(int i=1;i<=m;i++){
        int u,v;cin>>u>>v;
        edge[v].push_back(u);
        rev[u].push_back(v);
        inv[u]++;tinv[u]++;
        trev[v]++;
    }
    topo();
    
    for(int i=1;i<=n;i++){
        if(inv[i]){
            //说明不是DAG,直接返回false
            print();
            return;
        }
    }
    calc1();
    calc2();
    for(int i=1;i<=n;i++){
        if(L[i]<=(n+1)/2 and R[i]<=(n+1)/2) ans[i] = 1;
    }
    print();
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int T;cin>>T;
    while(T--) solve();
    return 0;
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 0
Accepted
time: 3ms
memory: 18160kb

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
01001000111
111
11111111111
111111111111111
001001000000000
00100
01100
1111111
1000000000000
111101101
111111111
000011111011101
010111111
001100000
0100001001101
1111111111111
001000010000000
10010111011
001000000000100
11111111111
00100000011
11111
01000000110
11101110111
00000
1111...

result:

ok 66 lines