# 链【省选模拟赛】

## 建5个图的神烦分类讨论

Posted by yjjr's blog on March 13, 2018

# 分析

1. 如果全部为链式结构，那么输出n
2. 如果有一个环，那么输出环的大小
3. 如果存在多个环，那么无解

# code

brute

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define mem(x,num) memset(x,num,sizeof x)
#define reg(x) for(int i=last[x];i;i=e[i].next)
#define lson (x<<1)
#define rson (x<<1|1)
#define mid ((l+r)>>1)
#define pi acos(-1)
#define mp make_pair
#define pb push_back
#define pa pair<int,int>
#define fi first
#define se second
#define lowbit(x) ((x)&(-x))
using namespace std;
ll f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
inline ll getgcd(ll x,ll y){return !x?y:getgcd(y%x,x);}
inline ll getlcm(ll x,ll y){return x/getgcd(x,y)*y;}
inline ll qpow(int x,int y,int p){
ll re=1;
while(y){
if(y&1)re=(re*x)%p;
y>>=1,x=(x*x)%p;
}
return re;
}
const int maxn=1e5+6;
char ch[2];
int n,m,last[maxn],vis[maxn],cnt,du[maxn];
struct edge{int to,next,w,self;}e[maxn<<2];
void insert(int u,int v){
e[++cnt]=(edge){v,last[u],1,u};last[u]=cnt;
e[++cnt]=(edge){u,last[v],1,v};last[v]=cnt;
du[u]++;du[v]++;
}
void dfs(int x,int fr){
vis[x]++;if(vis[x]>=2)return;
for(int i=last[x];i;i=e[i].next){
if(e[i].w==-1||e[i].to==fr)continue;
dfs(e[i].to,x);
}
}
inline bool check(){
mem(vis,0);
rep(i,1,n){
if(du[i]>2)return 0;
}
rep(i,1,n)if(vis[i]==0){
dfs(i,0);//cout<<i<<"E"<<endl;
//rep(j,1,n)cout<<vis[j]<<' ';cout<<"TT\n";
}
//rep(i,1,n)cout<<vis[i]<<' ';cout<<endl;
rep(i,1,n)if(vis[i]>1)return 0;
return 1;
}
void query(){
int ans=0;
rep(i,1,n){
rep(j,1,cnt)if((e[j].self==i)||(e[j].to==i)){e[j].w=-1;du[e[j].self]--;du[e[j].to]--;}
if(check())ans++;//cout<<i<<'r'<<endl;
rep(j,1,cnt)if((e[j].self==i)||(e[j].to==i)){e[j].w=1;du[e[j].self]++;du[e[j].to]++;}
}
cout<<ans<<endl;
}
int main()
{
rep(i,1,m){
scanf("%s",ch);
if(ch[0]=='L'){
insert(u,v);
}
if(ch[0]=='Q')query();
}
return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define mem(x,num) memset(x,num,sizeof x)
#define reg(x) for(int i=last[x];i;i=e[i].next)
#define lson (x<<1)
#define rson (x<<1|1)
#define mid ((l+r)>>1)
#define pi acos(-1)
#define mp make_pair
#define pb push_back
#define pa pair<int,int>
#define fi first
#define se second
#define lowbit(x) ((x)&(-x))
using namespace std;
ll f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
inline ll getgcd(ll x,ll y){return !x?y:getgcd(y%x,x);}
inline ll getlcm(ll x,ll y){return x/getgcd(x,y)*y;}
inline ll qpow(int x,int y,int p){
ll re=1;
while(y){
if(y&1)re=(re*x)%p;
y>>=1,x=(x*x)%p;
}
return re;
}
const int maxn=1e5+6;
int n,m,tot=0,flag=0;
char ch[2];
struct Graph{
struct sets{
int fa[maxn],setsize[maxn];
sets(){rep(i,1,maxn)fa[i]=i,setsize[i]=1;}
inline int find(const int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
inline bool merge(const int x,const int y){
int r1=find(x),r2=find(y);
if(r1==r2)return 0;else fa[r1]=r2,setsize[r2]+=setsize[r1];return 1;
}
}set;
int size,cnt,node_erased,node_si,du[maxn];
Graph(){size=0;cnt=0;node_erased=-1;node_si=-1;}
void ins(const int u,const int v){
if(u!=node_erased&&v!=node_erased){
du[u]++,du[v]++;
if(!set.merge(u,v))cnt++,size=set.setsize[set.find(u)];
if(du[u]==3)node_si=u;
if(du[v]==3)node_si=v;
}
}
inline bool query(){return cnt==0&&node_si==-1;}
}G[5];
vector< int > p[maxn];
vector< pair < int,int > > e;
int main(){
while(m--){
scanf("%s",ch);
if(ch[0]=='Q'){
if(flag){
int ans=0;rep(i,1,4)if(G[i].query())ans++;
printf("%d\n",ans);
}else{
if(G[0].cnt==0)printf("%d\n",n);
else if(G[0].cnt==1)printf("%d\n",G[0].size);else puts("0");
}
}
if(ch[0]=='L'){
if(flag)rep(i,1,4)G[i].ins(u,v);
else{
p[u].pb(v);p[v].pb(u);e.pb(mp(u,v));G[0].ins(u,v);
if(G[0].node_si!=-1){
flag=1;int t=0;
G[++t].node_erased=G[0].node_si;
for(vector<int>::iterator it=p[G[0].node_si].begin();it!=p[G[0].node_si].end();it++)G[++t].node_erased=*it;
for(vector< pair<int,int> >::iterator it=e.begin();it!=e.end();it++)
rep(i,1,4)G[i].ins((*it).first,(*it).second);
}
}
}
}
return 0;
}