BZOJ 2303 方格染色(带权并查集)
生活随笔
收集整理的這篇文章主要介紹了
BZOJ 2303 方格染色(带权并查集)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
要使得每個2*2的矩形有奇數個紅色,如果我們把紅色記為1,藍色記為0,那么我們得到了這2*2的矩形里的數字異或和為1.
對于每個方格則有a(i,j)^a(i-1,j)^a(i,j-1)^a(i-1,j-1)=1.由這些方程可以推出對于每個方格:
如果i,j都是偶數,則有a(i,j)^a(1,1)^a(i,1)^a(1,j)=1.
否則,a(i,j)^a(1,1)^a(i,1)^a(1,j)=0.枚舉a(1,1)的染色情況。可以由a(i,j)的染色情況推出a(i,1)和a(1,j)是否顏色相同或者相反。
類似于a bug's life。那么把它們用帶權并查集維護后,染色的種數就是一些聯通塊的染色種數,因為這些聯通塊互相不影響。
那么總染色數就是2^(cnt-1).
?
# include <cstdio> # include <cstring> # include <cstdlib> # include <iostream> # include <vector> # include <queue> # include <stack> # include <map> # include <set> # include <cmath> # include <algorithm> using namespace std; # define lowbit(x) ((x)&(-x)) # define pi acos(-1.0) # define eps 1e-3 # define MOD 1000000000 # define INF 1000000000 # define mem(a,b) memset(a,b,sizeof(a)) # define FOR(i,a,n) for(int i=a; i<=n; ++i) # define FO(i,a,n) for(int i=a; i<n; ++i) # define bug puts("H"); # define lch p<<1,l,mid # define rch p<<1|1,mid+1,r # define mp make_pair # define pb push_back typedef pair<int,int> PII; typedef vector<int> VI; # pragma comment(linker, "/STACK:1024000000,1024000000") typedef long long LL; int Scan() {int x=0,f=1;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; } void Out(int a) {if(a<0) {putchar('-'); a=-a;}if(a>=10) Out(a/10);putchar(a%10+'0'); } const int N=200005; //Code begin...struct Node{int l, r, c;}node[N]; int fa[N], dis[N];LL pow_mod(LL a, LL n, LL mod){LL ret=1, temp=a%mod;while (n) {if (n&1) ret=ret*temp%mod;temp=temp*temp%mod;n>>=1;}return ret; } int find(int x){int tmp;if (fa[x]!=x) tmp=find(fa[x]), dis[x]=dis[x]^dis[fa[x]], fa[x]=tmp;return fa[x]; } bool union_set(int x, int y, int d){int u=find(x), v=find(y);if (u!=v) {dis[u]=dis[y]^d^dis[x]; fa[u]=v; return true;}else return dis[x]^dis[y]^d==0; } int main () {int n, m, k, u, v, flag;LL ans=0;scanf("%d%d%d",&n,&m,&k);FOR(i,1,k) scanf("%d%d%d",&node[i].l,&node[i].r,&node[i].c);mem(dis,0); flag=true; FOR(i,1,n+m-1) fa[i]=i;FOR(i,1,k) {if (node[i].l==1) u=1;else u=m+node[i].l-1;v=node[i].r;if (node[i].l%2==0&&node[i].r%2==0) {if (!union_set(u,v,node[i].c^1)) {flag=false; break;}}else {if (!union_set(u,v,node[i].c)) {flag=false; break;}}}if (flag) {int cnt=0;FOR(i,1,n+m-1) if (fa[i]==i) ++cnt;ans=(ans+pow_mod(2,cnt-1,MOD))%MOD;}mem(dis,0); flag=true; FOR(i,1,n+m-1) fa[i]=i;FOR(i,1,k) {if (node[i].l==1) u=1;else u=m+node[i].l-1;v=node[i].r;if (node[i].l%2==0&&node[i].r%2==0) {if (!union_set(u,v,node[i].c)) {flag=false; break;}}else {if (!union_set(u,v,node[i].c^1)) {flag=false; break;}}}if (flag) {int cnt=0;FOR(i,1,n+m-1) if (fa[i]==i) ++cnt;ans=(ans+pow_mod(2,cnt-1,MOD))%MOD;}printf("%lld\n",ans);return 0; } View Code?
轉載于:https://www.cnblogs.com/lishiyao/p/6733565.html
總結
以上是生活随笔為你收集整理的BZOJ 2303 方格染色(带权并查集)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Gibbs sampling
- 下一篇: rtems的GNU(GCC)编译环境配置