QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#201813#5160. Kebab Pizzasalvator_noster#WA 1ms3948kbC++171.6kb2023-10-05 16:54:112023-10-05 16:54:11

Judging History

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

  • [2023-10-05 16:54:11]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3948kb
  • [2023-10-05 16:54:11]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

const int M=1e5+10;
typedef pair<int,int> pii;
pii piz[M];
int cnt[M],deg[M];
char loop;
int fath[M];
bool self_loop[M];

int Find(int x) {return x == fath[x] ? x : fath[x] = Find(fath[x]);}

int main(){
	int n,k;
	scanf("%d%d",&n,&k);
    set <int> sss;
	for(int i=1;i<=n;++i){
		int a,b;
		scanf("%d%d",&a,&b);
        sss.insert(a); sss.insert(b);
		if(a==b)self_loop[a]=1;
		if(a>b)swap(a,b);
		piz[i]=make_pair(a,b);
	}
    for (int i = 1; i <= k; i ++) fath[i] = i;
	sort(piz+1,piz+n+1);
	n=unique(piz+1,piz+n+1)-piz-1;
	for(int i=1;i<=n;++i){
		auto [a,b]=piz[i];
		if(a==b)continue;
		cnt[a]++,cnt[b]++;
	}
	for(int i=1;i<=n;++i){
		auto [a,b]=piz[i];
		if(a==b)continue;
		if(cnt[b]>=2)deg[a]++;
		if(cnt[a]>=2)deg[b]++;
	}
	for(int i=1;i<=n;++i){
		auto [a,b]=piz[i];
		if(a==b)continue;
		if(self_loop[a]&&cnt[a]==1&&deg[b]==2){
			puts("impossible");
			return 0;
		}
		swap(a,b);
		if(self_loop[a]&&cnt[a]==1&&deg[b]==2){
			puts("impossible");
			return 0;
		}
	}
	for(int i=1;i<=k;++i){
		if(deg[i]>2){
			puts("impossible");
			return 0;
		}
	}
    for (int i = 1; i <= n; i ++) {
        auto [a,b] = piz[i];
        if (a == b) continue;
        a = Find(a); b = Find(b);
        if (a == b) {
            loop = 1;
        }
        fath[a] = b;
    }
    if (loop) {
        set <int> s;
        for (int i = 1; i <= k; i ++)
            if (sss.count(i)) s.insert(Find(i));
        if (s.size() != 1) {
            puts("impossible");
			return 0;
        }
    }
	puts("possible");
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 0ms
memory: 3728kb

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: 0ms
memory: 3620kb

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: 1ms
memory: 3724kb

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: 0ms
memory: 3612kb

input:

4 4
1 2
2 1
3 4
4 3

output:

possible

result:

ok single line: 'possible'

Test #6:

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

input:

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

output:

possible

result:

ok single line: 'possible'

Test #7:

score: -100
Wrong Answer
time: 0ms
memory: 3632kb

input:

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

output:

possible

result:

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