求助
  • 板块灌水区
  • 楼主absolute_value
  • 当前回复2
  • 已保存回复2
  • 发布时间2024/11/26 17:41
  • 上次更新2024/11/26 20:03:06
查看原帖
求助
1067907
absolute_value楼主2024/11/26 17:41

也是被模板题教训上了:(

https://www.luogu.com.cn/problem/P4782

#include<bits/stdc++.h>
using namespace std;
int n,m,cnt=0;
vector<int> g[2000001];
int dfn[2000001]={0},low[2000001]={0},c=0,cc[2000001]={0};
bool ans=true;
stack<int> s;
void tarjan(int x) {
	if(!ans) return;
	dfn[x]=low[x]=++cnt;
	s.push(x);
	for(int i=0;i<g[x].size();i++){
		int y=g[x][i];
		if(dfn[y]==0){
			tarjan(y);
			low[x]=min(low[x],low[y]);
		}else{
			low[x]=min(low[x],dfn[y]);
		}
	}
	if(low[x]==dfn[x]){
		c++;
		while(s.top()!=x){
			if((s.top()<=n&&cc[s.top()+n]==c)||(s.top()>n&&cc[s.top()-n]==c)){
				ans=false;
				return;
			}
			cc[s.top()]=c;
			s.pop();
		}
		if((x<=n&&cc[s.top()+n]==c)||(x>n&&cc[s.top()-n]==c)){
			ans=false;
			return;
		}
		cc[x]=c;
		s.pop();
	}
}
int main() {
	cin>>n>>m;
	for(int i=0;i<m;i++) {
		int a,b,c,d;
		cin>>a>>b>>c>>d;
		g[a+b*n].push_back(c+(1-d)*n);
		g[c+d*n].push_back(a+(1-b)*n);
	}
	for(int i=1;i<=2*n;i++) {
		if(dfn[i]==0) {
			tarjan(i);
		}
	}
	if(ans) {
		cout<<"POSSIBLE\n";
		for(int i=1;i<=n;i++) {
			cout<<(cc[i]>cc[i+n])<<' ';
		}
	}else{
		cout<<"IMPOSSIBLE";
	}
	return 0;
}
2024/11/26 17:41
加载中...