QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#669342#7898. I Just Want... One More...Corycle#WA 4ms18144kbC++203.6kb2024-10-23 18:16:222024-10-23 18:16:24

Judging History

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

  • [2024-10-23 18:16:24]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:18144kb
  • [2024-10-23 18:16:22]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int inf=0x3f3f3f3f;
const int N=1e6+5;
ll read(){
    ll s=0,f=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){s=s*10+c-'0';c=getchar();}
    return s*f;
}
vector<int>G[N];
int n,m,S,T,cnt,tot;
int h[N],H[N],dis[N],vis[N],col[N],vst[N];
void Clear(){
    for(int i=1;i<=tot;i++){
        h[i]=0;
        vis[i]=0;
        col[i]=0;
        G[i].clear();
    }
    for(int i=1;i<=m;i++)vst[i]=0;
    cnt=1;
}
struct Edge{int x,y;}E[N]; 
struct edge{int to,next,flow;}d[N];
void Add(int x,int y,int z){
    d[++cnt]=(edge){y,h[x],z};h[x]=cnt;
    d[++cnt]=(edge){x,h[y],0};h[y]=cnt;
}
bool BFS(int s,int t){
    for(int i=1;i<=tot;i++){dis[i]=-1;H[i]=h[i];}
    queue<int>q;q.push(s);dis[s]=0;
    while(q.size()){
        int x=q.front();q.pop();
        if(x==t)return true;
        for(int i=h[x];i;i=d[i].next){
            int y=d[i].to;
            if(d[i].flow&&dis[y]==-1){
                dis[y]=dis[x]+1;
                q.push(y);
            }
        }
    }
    // cout<<1<<endl;
    return false;
}
int DFS(int x,int maxn,int t){
    if(x==t||!maxn)return maxn;
    int ans=0;
    for(int &i=H[x];i;i=d[i].next){
        int y=d[i].to;
        if(d[i].flow&&dis[y]==dis[x]+1){
            int dlt=DFS(y,min(maxn,d[i].flow),t);
            if(!dlt)dis[y]=-1;
            d[i].flow-=dlt;
            d[i^1].flow+=dlt;
            maxn-=dlt;ans+=dlt;
            if(!maxn)return ans;
        }
    }
    return ans;
}
int Dinic(int s,int t){
    int ans=0;
    while(BFS(s,t)){
        // cout<<"1111"<<endl;
        ans+=DFS(s,inf,t);
    }
    return ans;
}
void Calc(int x,int c){
    col[x]=c;
    for(auto y:G[x]){
        if(col[y])continue;
        Calc(y,c);
    }
}
int main(){
    int Case=read();
    while(Case--){
        cnt=1;
        n=read();m=read();S=n*2+1;T=n*2+2;tot=n*2+2;
        for(int i=1;i<=m;i++){
            int x=read(),y=read();
            Add(x,y+n,1);
            E[i].x=x;E[i].y=y;
        }
        for(int i=1;i<=n;i++){
            Add(S,i,1);
            Add(i+n,T,1);
        }
        Dinic(S,T);
        for(int x=1;x<=n;x++){
            for(int i=h[x];i;i=d[i].next){
                int y=d[i].to;
                if(y>n&&y<=2*n&&d[i].flow==0){
                    vst[i>>1]=1;
                    vis[x]=vis[y]=1;
                }
            }
        }
        // for(int i=1;i<=m;i++)cout<<vst[i]<<" ";cout<<endl;
        // for(int i=1;i<=n*2;i++)cout<<vis[i]<<" ";cout<<endl;
        int num1=0,num2=0,num3=0,num4=0;

        for(int i=1;i<=tot;i++){col[i]=0;G[i].clear();}
        for(int i=1;i<=m;i++){
            if(vst[i])G[E[i].y+n].push_back(E[i].x);
            else G[E[i].x].push_back(E[i].y+n);
        }
        for(int i=1;i<=n;i++)if(!vis[i])G[S].push_back(i);
        Calc(S,1);
        for(int i=1;i<=n;i++)if(col[i]==1)num1++;
        for(int i=n+1;i<=n*2;i++)if(col[i]==1)num2++;

        for(int i=1;i<=tot;i++){col[i]=0;G[i].clear();}
        for(int i=1;i<=m;i++){
            if(vst[i])G[E[i].x].push_back(E[i].y+n);
            else G[E[i].y+n].push_back(E[i].x);
        }
        for(int i=n+1;i<=n*2;i++)if(!vis[i])G[T].push_back(i);
        Calc(T,2);
        for(int i=1;i<=n;i++)if(col[i]==2)num3++;
        for(int i=n+1;i<=n*2;i++)if(col[i]==2)num4++;
        
        // cout<<num1<<" "<<num2<<" "<<num3<<" "<<num4<<endl;
        printf("%lld\n",1ll*num1*num4+1ll*num2*num3);

        Clear();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

6
0
4

result:

ok 3 number(s): "6 0 4"

Test #2:

score: -100
Wrong Answer
time: 4ms
memory: 18144kb

input:

10000
5 4
2 2
3 1
5 3
1 1
1 1
1 1
1 1
1 1
2 2
2 2
1 2
1 1
1 1
1 1
1 1
1 1
1 1
2 3
2 1
1 2
1 1
5 5
1 4
2 2
3 1
1 3
1 2
2 4
2 2
2 1
1 2
1 1
5 1
5 3
3 1
2 2
1 1
1 1
3 2
3 1
2 1
5 2
1 2
2 3
5 3
1 5
4 2
1 2
4 1
1 1
2 3
1 1
2 2
2 1
4 1
1 4
3 1
1 1
1 1
1 1
2 1
2 2
3 3
1 3
2 3
2 2
3 3
1 3
3 3
1 2
3 3
2 2
1 ...

output:

6
0
0
2
0
0
0
0
6
0
16
4
0
6
9
9
9
0
9
4
0
1
1
1
0
4
16
12
3
2
16
0
2
2
20
1
0
0
0
0
16
4
4
16
4
9
0
9
0
2
3
0
9
4
9
16
20
0
0
1
12
0
1
2
0
0
1
0
0
2
2
5
0
12
1
0
0
2
1
2
2
3
0
5
1
6
0
0
0
0
9
16
2
0
1
2
0
12
2
4
0
12
1
1
9
4
6
9
9
12
3
16
15
16
9
4
9
0
1
16
9
10
1
9
16
9
12
4
9
2
0
4
0
6
0
3
0
0
0
...

result:

wrong answer 72nd numbers differ - expected: '4', found: '5'