QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#338096#4513. Slide ParadeDualqwq0 2374ms46740kbC++173.2kb2024-02-25 17:45:152024-02-25 17:45:16

Judging History

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

  • [2024-02-25 17:45:16]
  • 评测
  • 测评结果:0
  • 用时:2374ms
  • 内存:46740kb
  • [2024-02-25 17:45:15]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int N = 5e2 + 5,M = 4e4 + 5;
int n,m;
int U[M],V[M];
int fir[N],nxt[M],st[M],to[M],ect = 1;
inline void addedge(int u1,int v1) {
	nxt[++ect] = fir[u1];fir[u1] = ect;to[ect] = v1;st[ect] = u1;
}
bool vis[N];
int mth[N],mthe[N];
bool dfs(int x) {
	for(int i = fir[x],y;y = to[i],i;i = nxt[i])
		if(!vis[y]) {
			vis[y] = 1;
			if(!mth[y] || dfs(mth[y])) { mth[x] = y;mth[y] = x;mthe[x] = i;return 1;}
		}
	return 0;
}
int pv[N],pe[N];
void Bfs(int st) {
	queue<int> Q;
	for(int i = 1;i <= n + n;i++) vis[i] = pv[i] = pe[i] = 0;
	Q.push(st);vis[st] = true;
	while(!Q.empty()) {
		int x = Q.front();Q.pop();
		// printf("st,x:%d,%d\n",st,x);
		if(x > n) {
			int y = mth[x];if(vis[y]) continue;
			pv[y] = x;pe[y] = mthe[y];
			Q.push(y);vis[y] = true;
		} else 
			for(int i = fir[x],y;y = to[i],i;i = nxt[i])
				if(i != mthe[x] && !vis[y]) {
					vis[y] = true;
					pv[y] = x;pe[y] = i;
					Q.push(y);
				} 
	}
}
vector<int> Ans;
namespace Euler {
	const int N = 1e3 + 5,M = 4e6 + 5;
	int fir[N],nxt[M],to[M],ect = 1,sgn[M];
	inline void addvir(int u1,int v1) {
		// printf("vir:%d->%d\n",u1,v1);
		nxt[++ect] = fir[u1];fir[u1] = ect;to[ect] = v1;sgn[ect] = 0;
	}
	void Euler(int x) {
		for(int i = fir[x],y;y = to[i],i;i = fir[x]) {
			fir[x] = nxt[i];
			if(sgn[i]) continue;
			sgn[i] =  true;
			Euler(y);
		}
		Ans.push_back(x);
	}
	void Clr(int n) {
		ect = 1;
		for(int i = 1;i <= n;i++) fir[i] = 0;
		Ans.clear();
	}
}
bool inmth[N];
void work() {
	cin >> n >> m;
	for(int i = 1;i <= m;i++) cin >> U[i] >> V[i];
	for(int i = 1;i <= n + n;i++) 
		pv[i] = pe[i] = mth[i] = vis[i] = mthe[i] = fir[i] = 0;
	Euler::Clr(n);
	ect = 1;
	for(int i = 1;i <= m;i++)
		addedge(U[i],V[i] + n);
	for(int i = 1;i <= n;i++) {
		for(int j = 1;j <= n + n;j++) vis[j] = 0;
		if(!dfs(i)) return puts("IMPOSSIBLE"),void();
	}
	for(int i = 1;i <= ect;i++) inmth[i] = 0;
	for(int i = 1;i <= n;i++) inmth[mthe[i]] = true,Euler::addvir(i,mth[i] - n);
	for(int i = 1;i <= m;i++) {
		int u = U[i],v = V[i] + n;
		if(mth[u] != v) {
			mth[mth[u]] = 0;
			for(int j = 1;j <= n + n;j++) vis[j] = 0;
			vis[v] = 1;
			if(!dfs(mth[v])) return puts("IMPOSSIBLE"),void();
			else mth[u] = v,mth[v] = u;
		} 
		for(int j = 1;j <= n;j++)
			Euler::addvir(j,mth[j] - n);
	}
	// for(int i = 1;i <= n;i++) {
	// 	Bfs(n + i);
	// 	for(int j = 1;j <= ect;j++)
	// 		if(!inmth[j] && to[j] == n + i) {
	// 			int u = st[j],v = n + i;
	// 			if(!vis[u]) return puts("IMPOSSIBLE"),void();
	// 			int now = u;
	// 			while(now != v) inmth[pe[now]] ^= 1,now = pv[now];
	// 			inmth[j] ^= 1;
	// 			for(int k = 1;k <= ect;k++)
	// 				if(inmth[k]) Euler::addvir(st[k],to[k] - n);
	// 			now = u;
	// 			while(now != v) inmth[pe[now]] ^= 1,now = pv[now];
	// 			inmth[j] ^= 1;
	// 		}
	// }
	Euler::Euler(1);
	printf("%d\n",(int)Ans.size());
	reverse(Ans.begin(),Ans.end());
	// Ans.push_back(1);
	for(auto i : Ans) printf("%d ",i);
	printf("\n");
}
int main() {
	int T;
	cin >> T;
	for(int _ = 1;_ <= T;_++) {
		printf("Case #%d: ",_);
		work();
	}
	return 0;
}

/*
1
3 6
1 2
1 3
2 1
2 3
3 1
3 2
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 9932kb

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

result:

wrong answer the slide from 3 to 4 hasn't be used (test case 25)

Test #2:

score: 0
Wrong Answer
time: 2374ms
memory: 46740kb

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: 1000199
1 193 80 66 24 105 108 168 90 64 13 28 199 176 70 129 114 65 97 92 15 151 34 62 43 38 53 82 175 25 136 47 147 161 112 123 57 8 37 22 16 50 166 127 156 20 42 149 11 73 140 145 146 125 137 99 14 101 17 52 157 138 200 169 191 102 154 148 159 122 ...

result:

wrong answer L (= 1000199) is out of range [5001, 1000001] (test case 3)