QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#201936#5160. Kebab PizzaTeam_name#WA 6ms24272kbC++201.9kb2023-10-05 17:44:142023-10-05 17:44:15

Judging History

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

  • [2023-10-05 17:44:15]
  • 评测
  • 测评结果:WA
  • 用时:6ms
  • 内存:24272kb
  • [2023-10-05 17:44:14]
  • 提交

answer

#include <bits/stdc++.h>

#define endl '\n'
#define DEBUG_VAR(x) { cerr << "* "#x" = " << x << endl; }
#define DEBUG_FMT(...) { cerr << "* "; fprintf(stderr, __VA_ARGS__); }

using namespace std;
using LL = long long;

const int N = 4e5+5;

int m, n;
int deg[N];

set<int> e[N];
// vector<int> e[N];
bool vis[N];

void topsort()
{
	queue<int> q;

	for(int i = 1; i <= n; i++) {
		if(deg[i] <= 1) {
			q.push(i);
			vis[i] = true;
		}
	}

	while(q.size()) {
		int x = q.front(); q.pop();

		for(int y : e[x]) {
			e[y].erase(x);
			deg[y]--;
			// if(deg[y] <= 1 && !vis[y]) {
			// 	q.push(y);
			// 	vis[y] = true;
			// }
		}
	}
}

bool ins[N];
int cnt = 0, cnt_times = 0;

void dfs(int x, int fa)
{
	if(ins[x]) cnt++;
	if(vis[x]) return;

	vis[x] = true;
	ins[x] = true;
	for(int y : e[x]) {
		if(fa == y) continue;
		dfs(y, x);
	}
	ins[x] = false;
}

int main()
{
	// freopen("1.in", "r", stdin);
	ios::sync_with_stdio(false); cin.tie(nullptr);

	cin >> m >> n;
	for(int i = 1; i <= m; i++) {
		int x, y;
		cin >> x >> y;
		if(x == y) {
			e[x].insert(x+n);
			e[x+n].insert(x);
		} else {
			e[x].insert(y); e[y].insert(x);
		}	
	}

	n = 2*n;
	for(int x = 1; x <= n; x++) {
		// for(int y : e[x]) {
		// 	// e[x].push_back(y);
		// 	deg[x]++;
		// }
		deg[x] = e[x].size();
	}

	topsort();

	int cur = 0;
	for(int i = 1; i <= n; i++) {
		if(!vis[i]) {
			cur = i;
			break;
		}
	}
	if(!cur) {
		cout << "possible" << endl;
		return 0;
	}

	for(int i = 1; i <= n; i++) {
		if(vis[i]) continue;
		if(deg[i] > 2) {
			cout << "impossible" << endl;
			return 0;
		}
	}

	for(int i = 1; i <= n; i++) {
		if(!vis[i]) {
			dfs(i, -1);
			cnt_times++;
		}
	}

	if(cnt == 0 || (cnt_times == cnt && cnt == 1)) {
		cout << "possible" << endl;
		return 0;
	} else {
		cout << "impossible" << endl;
		return 0;
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 23060kb

input:

7 6
2 2
3 6
1 1
1 5
4 5
6 6
6 5

output:

possible

result:

ok single line: 'possible'

Test #2:

score: 0
Accepted
time: 6ms
memory: 24008kb

input:

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

output:

possible

result:

ok single line: 'possible'

Test #3:

score: 0
Accepted
time: 6ms
memory: 22876kb

input:

6 7
1 2
2 3
3 4
4 5
3 6
6 7

output:

impossible

result:

ok single line: 'impossible'

Test #4:

score: 0
Accepted
time: 0ms
memory: 22380kb

input:

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

output:

possible

result:

ok single line: 'possible'

Test #5:

score: 0
Accepted
time: 6ms
memory: 24272kb

input:

4 4
1 2
2 1
3 4
4 3

output:

possible

result:

ok single line: 'possible'

Test #6:

score: 0
Accepted
time: 3ms
memory: 22924kb

input:

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

output:

possible

result:

ok single line: 'possible'

Test #7:

score: 0
Accepted
time: 0ms
memory: 24100kb

input:

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

output:

impossible

result:

ok single line: 'impossible'

Test #8:

score: -100
Wrong Answer
time: 6ms
memory: 22916kb

input:

4 5
1 2
3 4
4 5
5 3

output:

possible

result:

wrong answer 1st lines differ - expected: 'impossible', found: 'possible'