CF27D||Ring Road 2
题目大意
给你 Impossible。否则请输出任意一种连边方案。
大体思路
既然本蒟蒻都可以不看题解切出来,那思路一定是很简单的。
首先,先分析一下这道题在考什么。对于
如果你不知道 2-SAT 是什么,可以去P4782 【模板】2-SAT,也可以查看这篇蒟蒻写的学习笔记。
知道了它在考察什么,剩下的就好办得多了。首先,对于相交的处理,先看一眼
剩下的内容就是很板的 2-SAT 了,不在此赘述。
代码演示
大体思路就是这样,如果还有不清楚的地方,可以结合代码理解。
#include<iostream>
#include<cstring>
#define int long long
using namespace std;
struct Edge{
int l,nxt;
}edges[2005];
int n,m,tt=1,head[205],vu[205];
int tot,dfn[205],low[205];
int top,instk[205],stk[205];
int cnt,scc[205];
pair<int,int> po[205];
string s;
void add_edge(int f,int l){
tt+=1;
edges[tt]={l,head[f]};
head[f]=tt;
}
void tarjian(int x){
tot+=1,top+=1;
dfn[x]=low[x]=tot;
stk[top]=x,instk[x]=1;
for(int i=head[x];i;i=edges[i].nxt){
int l=edges[i].l;
if(!low[l]){
tarjian(l);
low[x]=min(low[x],low[l]);
}
else if(instk[l]) low[x]=min(low[x],dfn[l]);
}
if(dfn[x]==low[x]){
int y;cnt+=1;
do{
y=stk[top],top-=1,instk[y]=0;
scc[y]=cnt;
}while(y!=x);
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n>>m;
for(int i=1;i<=m;i++) cin>>po[i].first>>po[i].second;
for(int i=1;i<=m;i++){
memset(vu,0,sizeof(vu));
int a=po[i].first,b=po[i].second;
for(int i=min(a,b);i<=max(a,b);i++) //进行染色
vu[i]=1;
for(int j=i+1;j<=m;j++){
if(i==j) continue;
int c=po[j].first,d=po[j].second;
if(vu[c]!=vu[d]&&c!=a&&d!=a&&c!=b&&d!=b){ //如果相交
add_edge(i,j+m);add_edge(j,i+m);
add_edge(i+m,j);add_edge(j+m,i); //分别枚举i/j在内/外侧的情况
}
}
}
for(int i=1;i<=m*2;i++) if(!low[i]) tarjian(i);
for(int i=1;i<=m;i++){
if(scc[i]==scc[i+m]){
cout<<"Impossible"<<endl;
return 0;
}
else if(scc[i]<scc[i+m]) s+='i';
else s+='o';
}
cout<<s;
return 0;
}