QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#56864#4818. Inverse Line GraphKING_UT#WA 5ms10852kbC++203.9kb2022-10-21 18:57:142022-10-21 18:57:15

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-10-21 18:57:15]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:10852kb
  • [2022-10-21 18:57:14]
  • 提交

answer

#include <bits/stdc++.h>
#define SIZE 300005
#define MX 3000005
using namespace std;
typedef pair <int,int> P;

vector <P> vec[SIZE];
int col[MX];
set <P> edge;
bool use[SIZE];
int now_sz;
bool clique(vector <int> nd){
	for(int i=0;i<nd.size();i++){
		for(int j=i+1;j<nd.size();j++){
			int a=nd[i],b=nd[j];
			if(a>b) swap(a,b);
			if(edge.find(P(a,b))==edge.end()) return false;
		}
	}
	return true;
}
bool cur[SIZE];
bool labelling(vector <int> nd,int now){
	//for(int i=0;i<nd.size();i++) printf("%d ",nd[i]);
	//printf(": %d\n",now);
	for(int i=0;i<nd.size();i++) cur[nd[i]]=true;
	bool up=true;
	for(int i=0;i<nd.size();i++){
		int v=nd[i];
		for(int j=0;j<vec[v].size();j++){
			int to=vec[v][j].first,id=vec[v][j].second;
			if(cur[to]){
				//printf("[%d %d] %d\n",v,to,col[id]);
				if(col[id]!=-1&&col[id]!=now) up=false;
				col[id]=now;
			}
		}
	}
	for(int i=0;i<nd.size();i++) cur[nd[i]]=false;
	return up;
}
bool dfs(vector <int>nd);
bool check_vertex(int v);
int now_col;
bool dfs(vector <int> nd){
	//for(int i=0;i<nd.size();i++) printf("%d ",nd[i]);puts("");
	if(!clique(nd)) return false;
	if(!labelling(nd,now_col++)) return false;
	for(int i=0;i<nd.size();i++){
		int v=nd[i];
		if(!check_vertex(v)) return false;
	}
	return true;
}
bool check_vertex(int v){
	//printf("* %d : ",v);
	set <int> st;
	vector <int> nd;
	for(int i=0;i<vec[v].size();i++){
		int to=vec[v][i].first,id=vec[v][i].second;
		//printf("%d ",col[id]);
		if(col[id]!=-1){
			st.insert(col[id]);
			if(st.size()>2) return false;
		} else{
			nd.push_back(to);
		}
	}
	//puts("");
	if(!nd.empty()){
		nd.push_back(v);
		if(!dfs(nd)) return false;
	}
	return true;
}
bool start(vector <int> nd){
	int v=nd[0];
	int f=now_col;
	queue <int> que;
	que.push(v);
	use[v]=true;
	vector <int> all;
	while(!que.empty()){
		v=que.front();que.pop();
		all.push_back(v);
		for(int i=0;i<vec[v].size();i++){
			int to=vec[v][i].first,id=vec[v][i].second;
			col[id]=-1;
			if(!use[to]){
				que.push(to);
				use[to]=true;
			}
		}
	}
	if(!dfs(nd)){
		now_col=f;
		for(int i=0;i<all.size();i++) use[all[i]]=false;
		return false;
	}
	return true;
}
bool run(int v){
	if(vec[v].size()==0) return true;
	if(vec[v].size()<=4){
		return start(vector <int>{0,1});
		for(int S=0;S<1<<vec[v].size();S++){
			if(!(S>>0&1)) continue;
			vector <int> nd;
			nd.push_back(v);
			for(int i=0;i<vec[v].size();i++){
				if(S>>i&1){
					nd.push_back(vec[v][i].first);
				}
			}
			if(start(nd)){
				return true;
			}
		}
		return false;
	}
	int m=5;
	vector <int> mx;
	for(int S=0;S<1<<m;S++){
		vector <int> nd;
		for(int i=0;i<m;i++){
			if(S>>i&1){
				nd.push_back(vec[v][i].first);
			}
		}
		if(mx.size()<nd.size()&&clique(nd)){
			mx=nd;
		}
	}
	if(mx.size()<=2) return false;
	vector <int> one=mx;
	one.push_back(v);
	for(int i=m;i<vec[v].size();i++){
		int to=vec[v][i].first;
		vector <int> nd=mx;
		nd.push_back(to);
		if(clique(nd)){
			one.push_back(to);
		}
	}
	return start(one);
}
void solve(){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++) vec[i].clear();
	edge.clear();
	for(int i=0;i<m;i++){
		int u,v;
		scanf("%d%d",&u,&v);u--,v--;
		vec[u].push_back(P(v,i));
		vec[v].push_back(P(u,i));
		edge.insert(P(min(u,v),max(u,v)));
	}
	for(int i=0;i<n;i++) use[i]=false;
	for(int i=0;i<m;i++) col[i]=-1;
	now_sz=0;
	now_col=0;
	for(int i=0;i<n;i++){
		if(use[i]) continue;
		if(!run(i)){
			puts("No");
			return;
		}
	}
	puts("Yes");
	vector <int> L(n),R(n);
	for(int i=0;i<n;i++){
		set <int> st;
		for(int j=0;j<vec[i].size();j++){
			int id=vec[i][j].second;
			if(col[id]!=-1) st.insert(col[id]);
		}
		assert(st.size()<=2);
		while(st.size()<2){
			st.insert(now_col++);
		}
		set <int>::iterator it=st.begin();
		L[i]=*it;
		R[i]=*(++it);
	}
	printf("%d %d\n",now_col,n);
	for(int i=0;i<n;i++) printf("%d %d\n",L[i]+1,R[i]+1);
}
int main(){
	int T;
	scanf("%d",&T);
	while(T--) solve();
}
/*
 
Yes
5 5
1 2
1 4
2 5
2 3
3 4
Yes
2 1
1 2
Yes
3 2
1 2
1 3
Yes
3 3
1 2
1 3
2 3
No
Yes
5 5
1 5
1 2
2 3
2 4
3 4

 */

详细

Test #1:

score: 100
Accepted
time: 5ms
memory: 10852kb

input:

6
5 6
1 2
1 3
1 4
3 4
2 5
4 5
1 0
2 1
1 2
3 3
1 2
1 3
2 3
4 3
1 2
1 3
1 4
5 6
1 2
2 3
2 4
3 4
3 5
4 5

output:

Yes
5 5
1 2
1 4
2 5
2 3
3 4
Yes
2 1
1 2
Yes
3 2
1 2
1 3
Yes
3 3
1 2
1 3
2 3
No
Yes
5 5
1 5
1 2
2 3
2 4
3 4

result:

ok that's great! (sum n = 20, sum m = 19) (6 test cases)

Test #2:

score: -100
Wrong Answer
time: 1ms
memory: 10716kb

input:

5
6 5
3 4
1 6
1 5
2 5
5 6
2 0
1 0
5 3
2 3
3 4
2 4
6 4
5 6
4 5
1 6
2 4

output:

No
Yes
4 2
1 2
3 4
Yes
2 1
1 2
No
No

result:

wrong answer you didn't find a solution but jury did (test case 1)