QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#425041#4513. Slide Paradetarjen0 3146ms104980kbC++203.5kb2024-05-29 21:39:452024-05-29 21:39:46

Judging History

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

  • [2024-05-29 21:39:46]
  • 评测
  • 测评结果:0
  • 用时:3146ms
  • 内存:104980kb
  • [2024-05-29 21:39:45]
  • 提交

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;
void getpath(int n,vector<pair<int,int>> &edges){
    vector<vector<int>> ve(n+1);
    for(auto [x,y]:edges)ve[x].push_back(y);
    vector<int> ans;
    function<void(int)> dfs = [&](int u)//????
    {
        while(!ve[u].empty())
        {
            int v=ve[u].back();ve[u].pop_back();
            dfs(v);
        }
        ans.push_back(u);
    };
    dfs(1);
    cout<<ans.size()<<"\n";
    for(auto it:ans)cout<<it<<" ";;cout<<endl;
}
void solve()
{
    int n;cin>>n;
    int m;cin>>m;
    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);
        }
    }
    getpath(n,all);
}
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: 0
Wrong Answer
time: 1ms
memory: 3684kb

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 5 2 4 3 1 3 5 2 4 1 5 3 2 4 1 3 5 2 4 1 3 5 2 4 1 5 2 4 3 1 5 3 2 4 1 3 5 2 4 1 5 2 4 3 1 3 5 2 4 1 
Case #3: 51
1 3 2 5 4 1 3 2 5 2 5 4 1 3 4 1 3 2 5 4 1 5 3 2 4 1 5 3 2 5 2 4 1 3 4 1 3 2 5 4 1 5 3 2 4 1 3 2 5 4 1 
Case #4: 19
1 3 2 1 2 3 1 2 3 1 3 2 1 3 2 1 2 3 1 ...

result:

wrong answer invalid move (from 1 to 5) (test case 2)

Test #2:

score: 0
Wrong Answer
time: 3146ms
memory: 104980kb

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 151 170 163 174 184 182 194 193 198 176 9 185 197 195 196 189 192 172 180 145 152 142 106 89 115 116 130 119 124 98 83 63 45 33 49 47 64 39 61 76 67 59 72 58 55 77 71 87 73 88 90 69 70 31 41 30 56 66 85 68 75 65 62 57 46 74 52 53 38 48 42 24...

result:

wrong answer invalid move (from 1 to 151) (test case 3)