题解 交通

传送门

发现每个点有且仅有两条入边和两条出边
发现每队入边和出边不能同时被删除
于是以原边为点建新图,在不能同时删除的点间连边
于是每个点度数为2,且形成许多偶环
每个偶环有2种选法,于是为 \(2^{环数}\)

Code:
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 200010
#define ll long long
#define fir first
#define sec second
#define make make_pair
#define pb push_back
//#define int long long

char buf[1<<21], *p1=buf, *p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf, 1, 1<<21, stdin)), p1==p2?EOF:*p1++)
inline int read() {
	int ans=0, f=1; char c=getchar();
	while (!isdigit(c)) {if (c=='-') f=-f; c=getchar();}
	while (isdigit(c)) {ans=(ans<<3)+(ans<<1)+(c^48); c=getchar();}
	return ans*f;
}

int n;
const ll mod=998244353;

namespace force{
	int in[N], out[N], ans;
	pair<int, int> e[N];
	void solve() {
		for (int i=1,u,v; i<=n*2; ++i) {
			u=read()-1; v=read()-1;
			e[i-1]=make(u, v);
		}
		int lim=(1<<(n*2));
		for (int s=1,s2,cnt; s<lim; ++s) {
			s2=s; cnt=0;
			do {++cnt; s2&=s2-1;} while (s2);
			if (cnt!=n) goto jump;
			for (int i=0; i<n; ++i) in[i]=out[i]=0;
			for (int i=0; i<n*2; ++i) if (s&(1<<i)) {
				++out[e[i].fir]; ++in[e[i].sec];
			}
			for (int i=0; i<n; ++i) if (in[i]!=1 || out[i]!=1) goto jump;
			++ans;
			jump: ;
		}
		printf("%d\n", ans);
	}
}

namespace task1{
	int fa[N];
	ll ans=1ll;
	bool vis[N];
	vector<int> in[N], out[N];
	inline int find(int p) {return fa[p]==p?p:fa[p]=find(fa[p]);}
	void solve() {
		for (int i=1; i<=n*2; ++i) fa[i]=i;
		for (int i=1,u,v; i<=n*2; ++i) {
			u=read(); v=read();
			out[u].pb(i); in[v].pb(i);
		}
		for (int i=1; i<=n; ++i) {
			fa[find(in[i][0])]=find(in[i][1]);
			fa[find(out[i][0])]=find(out[i][1]);
		}
		for (int i=1; i<=n*2; ++i) if (!vis[find(i)]) {
			ans=ans*2%mod;
			vis[find(i)]=1;
		}
		printf("%lld\n", ans);
		exit(0);
	}
}

signed main()
{
	freopen("a.in", "r", stdin);
	freopen("a.out", "w", stdout);

	n=read();
	// force::solve();
	task1::solve();
	
	return 0;
}
posted @ 2021-09-25 19:35  Administrator-09  阅读(4)  评论(0编辑  收藏  举报