QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#201922#5160. Kebab Pizzawhsyhyyh#WA 3ms16740kbC++142.0kb2023-10-05 17:40:332023-10-05 17:40:33

Judging History

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

  • [2023-10-05 17:40:33]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:16740kb
  • [2023-10-05 17:40:33]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+9;
int n,k,m,cnt[N];
struct pp{int a,b;}p[N];
bool cmp(pp x,pp y){
    return x.a==y.a?(x.b<y.b):(x.a<y.a);
}
int head[N],tot;
struct ptp{int nxt,to;}g[N<<1];
void add(int u,int v){
	g[++tot].nxt=head[u],g[tot].to=v,head[u]=tot;
}
int vis[N];
void dfs(int u,int fa){
	if(vis[u]) return ;
	vis[u]=1;
	for(int i=head[u];i!=-1;i=g[i].nxt){
		int v=g[i].to;if(v==fa) continue;
		dfs(v,u);
	}
	return ;
}
int main(){
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++){
         scanf("%d%d",&p[i].a,&p[i].b);
         if(p[i].a>p[i].b) swap(p[i].a,p[i].b);
    }
    sort(p+1,p+n+1,cmp);
    m=0;
    for(int i=1;i<=n;i++){
        if(p[i].a==p[i-1].a&&p[i].b==p[i-1].b) continue;
        p[++m]=p[i];
    }
    n=m;
    for(int i=1;i<=n;i++) cnt[p[i].a]++,cnt[p[i].b]++;
    int sb=0;
    for(int i=1;i<=n;i++){
    	if(cnt[p[i].a]==1&&cnt[p[i].b]==1) sb++;
    	if(p[i].a==p[i].b&&cnt[p[i].a]==2) sb++;
	}
    memset(cnt,0,sizeof(cnt));
    m=0;
    for(int i=1;i<=n;i++){
        if(p[i].a==p[i].b) continue;
        if(p[i].a==p[i-1].a&&p[i].b==p[i-1].b) continue;
        p[++m]=p[i];
    }
    n=m;
    for(int i=1;i<=n;i++) cnt[p[i].a]++,cnt[p[i].b]++;
    m=0;
    for(int i=1;i<=n;i++){
        if(cnt[p[i].a]==1||cnt[p[i].b]==1) continue;
        p[++m]=p[i];
    }
    n=m;
    memset(cnt,0,sizeof(cnt));
    for(int i=1;i<=n;i++) cnt[p[i].a]++,cnt[p[i].b]++;
    int flag=1;
    for(int i=1;i<=k;i++) if(cnt[i]>=3) flag=0;
    if(!flag){
        printf("impossible\n");
        return 0;
    }
    memset(head,-1,sizeof(head));tot=0;
    for(int i=1;i<=n;i++){
    	add(p[i].a,p[i].b);
    	add(p[i].b,p[i].a);
	}
	flag=0;
	for(int i=1;i<=k;i++)
		if(!vis[i]&&cnt[i]==1){
			dfs(i,0);flag=1;
		}
	int scc=0;
	for(int i=1;i<=k;i++)
		if(!vis[i]&&cnt[i]==2){
			dfs(i,0);scc++;
		}
	if(scc==1&&!flag&&!sb) printf("possible\n");
	else if(scc==0) printf("possible\n");
	else printf("impossible\n");
    return 0;
}

详细

Test #1:

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

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: 16740kb

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: 2ms
memory: 11084kb

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: 15112kb

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: 2ms
memory: 15112kb

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: 13988kb

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: 2ms
memory: 15096kb

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'