QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#425077#4513. Slide Paradetarjen35 ✓3348ms106616kbC++203.9kb2024-05-29 21:56:042024-05-29 21:56:05

Judging History

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

  • [2024-05-29 21:56:05]
  • 评测
  • 测评结果:35
  • 用时:3348ms
  • 内存:106616kb
  • [2024-05-29 21:56:04]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int N=510,M=1e5; 
class Maxflow{
private:
	int nedge=1,p[2*M],nex[2*M],head[N],c[2*M],cur[2*M];
    int dist[2*N];
    int S,T;
	void Addedge(int a,int b,int v){
		p[++nedge]=b;nex[nedge]=head[a];head[a]=nedge;
		c[nedge]=v;
	}
    bool bfs(){
		queue<int>q;
		for(int i=S;i<=T;i++)dist[i]=-1;
		dist[S]=0;q.push(S);
		while(!q.empty()){
			int now=q.front();q.pop();
			for(int k=head[now];k;k=nex[k])if(dist[p[k]]==-1&&c[k]>0){
				dist[p[k]]=dist[now]+1;
				q.push(p[k]);
			}
		}
		return dist[T]>-1;
	}
	int dfs(int x,int low){
		if(x==T)return low;
		if(low==0)return 0;
		int used=0;
		for(int &k=cur[x];k;k=nex[k])if(dist[p[k]]==dist[x]+1&&c[k]>0){
			int a=dfs(p[k],min(c[k],low-used));
			c[k]-=a;c[k^1]+=a;used+=a;
			if(low==used)break;
		}
		if(used==0)dist[x]=-1;
		return used;
	}
public:
	void init(int s,int t){
		for(int i=S;i<=T;i++)head[i]=0;
		S=s,T=t;
		nedge=1;
	}
    void addedge(int a,int b,int v){
        Addedge(a,b,v);
        Addedge(b,a,0);
	}
	int dinic(vector<pair<int,int>> &match){
		int flow=0;
		while(bfs()){
			for(int i=S;i<=T;i++)cur[i]=head[i];
			flow+=dfs(S,1e9);
		}
        for(int i=1;i<=T/2;i++){
            for(int k=head[i];k;k=nex[k])if(p[k]>T/2&&p[k]<T&&c[k]==0){
                match.emplace_back(i,p[k]);
            }
        }
		return flow;
	}
}sol;
vector<int> getpath(int n,vector<pair<int,int>> &edges,bool &flag){
    vector<vector<int>> ve(n+1);
    for(auto &[x,y]:edges)ve[x].push_back(y);
    vector<int> ans;
    vector<int> vis(n+1);
    function<void(int)> dfs = [&](int u)//????
    {
        vis[u]=1;
        while(!ve[u].empty())
        {
            int v=ve[u].back();ve[u].pop_back();
            dfs(v);
        }
        ans.push_back(u);
    };
    dfs(1);
    flag=true;
    for(int i=1;i<=n;i++)flag&=vis[i];
    reverse(ans.begin(),ans.end());
    return ans;
}
const bool test=false;
void solve()
{
    int n,m;
    if(!test)cin>>n>>m;
    else n=10,m=20;
    int s=0,t=2*n+1;
    sol.init(s,t);
    vector<pair<int,int>> edges(m);
    vector<vector<int>> lis(n+1);
    for(int i=0;i<m;i++)cin>>edges[i].first>>edges[i].second,edges[i].second+=n,lis[edges[i].first].push_back(edges[i].second);
    for(int i=1;i<=n;i++)sol.addedge(s,i,1),sol.addedge(i+n,t,1);
    for(auto [x,y]:edges)sol.addedge(x,y,1);
    vector<pair<int,int>> match;
    if(sol.dinic(match)!=n){cout<<"IMPOSSIBLE\n";return;}
    vector<int> to(n+1);
    vector<pair<int,int>> all;
    for(auto [x,y]:match)to[x]=y;
    // cout<<"match :";for(auto [x,y]:match)cout<<x<<"/"<<y<<" ";;cout<<endl;
    vector<vector<int>> ve(2*n+1);
    for(auto [x,y]:edges){
        if(y==to[x])ve[x].push_back(y);
        else ve[y].push_back(x);
    }
    for(int rt=1;rt<=n;rt++){
        vector<int> fa(n*2+1,-1);
        queue<int> qu;qu.push(rt);
        while(!qu.empty()){
            int x=qu.front();qu.pop();
            for(auto it:ve[x])if(fa[it]==-1){
                fa[it]=x;
                qu.push(it);
            }
        }
        for(auto it:lis[rt]){
            if(fa[it]==-1){cout<<"IMPOSSIBLE\n";return;}
            auto tt=to;
            int x=it;
            vector<int> v;
            while(x!=rt){
                v.push_back(x);x=fa[x];
            }
            v.push_back(rt);
            reverse(v.begin(),v.end());
            for(int i=2;i<(int)v.size();i+=2)tt[v[i]]=v[i-1];
            tt[rt]=v.back();
            for(int i=1;i<=n;i++)all.emplace_back(i,tt[i]-n);
        }
    }
    bool flag=true;
    auto ans=getpath(n,all,flag);
    if(!flag){cout<<"IMPOSSIBLE\n";return;}
    for(int i=1;i<(int)ans.size();i++){
        int p=ans[i-1],q=ans[i];
        assert(find(lis[p].begin(),lis[p].end(),q+n)!=lis[p].end());
    }
    cout<<ans.size()<<"\n";
    for(auto it:ans)cout<<it<<" ";;cout<<endl;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    cin>>T;
    for(int ts=1;ts<=T;ts++){
        cout<<"Case #"<<ts<<": ";
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 11
Accepted
time: 0ms
memory: 5868kb

input:

100
5 8
1 2
1 3
1 4
1 5
2 1
3 1
4 1
5 1
5 10
1 3
1 4
2 3
2 5
3 1
3 4
3 5
4 2
5 1
5 3
5 10
1 4
2 3
2 5
3 1
3 5
4 2
4 3
4 5
5 1
5 2
3 6
1 2
1 3
2 1
2 3
3 1
3 2
5 10
1 2
1 5
2 3
2 4
3 1
4 3
4 5
5 2
5 3
5 4
4 10
1 2
1 3
1 4
2 1
2 3
2 4
3 1
3 4
4 2
4 3
5 10
1 2
1 3
2 1
2 4
3 1
3 5
4 2
4 5
5 3
5 4
5 10
1 ...

output:

Case #1: IMPOSSIBLE
Case #2: 51
1 4 2 5 3 1 3 4 2 5 1 4 2 5 3 1 4 2 3 5 1 3 4 2 5 1 4 2 5 3 1 4 2 5 3 1 4 2 3 5 1 4 2 5 3 1 3 4 2 5 1 
Case #3: 51
1 4 5 2 3 1 4 2 3 5 1 4 5 2 3 1 4 3 1 4 2 5 2 3 5 1 4 2 3 5 1 4 5 2 3 1 4 3 1 4 5 2 5 2 3 1 4 5 2 3 1 
Case #4: 19
1 3 2 1 2 3 1 2 3 1 3 2 1 3 2 1 2 3 1 ...

result:

ok  (100 test cases)

Test #2:

score: 24
Accepted
time: 3348ms
memory: 106616kb

input:

100
199 4980
1 95
1 96
1 105
1 124
1 130
1 131
1 135
1 137
1 139
1 140
1 141
1 147
1 155
1 172
1 174
1 179
1 183
1 188
1 193
1 196
1 197
2 3
2 5
2 13
2 14
2 17
2 20
2 24
2 26
2 30
2 41
2 44
2 45
2 52
2 56
2 67
2 70
2 71
2 74
2 78
2 84
2 85
2 90
2 92
2 93
2 97
2 107
2 111
2 113
2 122
2 124
2 128
2 13...

output:

Case #1: IMPOSSIBLE
Case #2: IMPOSSIBLE
Case #3: 1000001
1 143 132 111 110 108 122 113 97 92 93 120 103 118 121 123 128 135 136 149 154 140 127 133 141 114 112 102 95 81 78 50 51 37 22 40 190 187 6 2 156 177 173 188 199 200 169 164 162 148 165 157 138 134 117 99 91 101 86 82 80 84 79 94 96 107 105 1...

result:

ok  (100 test cases)