[色多项式] UOJ #308. 【UNR #2】UOJ拯救计划 & SRM 717 div1 AcyclicOrientation
来源:互联网 发布:采购询价软件 编辑:程序博客网 时间:2024/06/08 15:15
一个图的
称为色多项式
那么这里模6 我们只要知道模2和模3的值
然后分类讨论下就好了
一张图的0染色数是0,1染色数等于
#include<cstdio>#include<cstdlib>#include<algorithm>#include<cstring>#define cl(x) memset(x,0,sizeof(x))using namespace std;inline char nc(){ static char buf[100000],*p1=buf,*p2=buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;}inline void read(int &x){ char c=nc(),b=1; for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1; for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;}const int N=100005;struct edge{ int u,v,next;}G[N<<2];int head[N],inum;inline void add(int u,int v,int p){ G[p].u=u; G[p].v=v; G[p].next=head[u]; head[u]=p;}#define V G[p].vint fat[N],depth[N];int flag=0;inline void dfs(int u,int fa){ fat[u]=fa; depth[u]=depth[fa]+1; for (int p=head[u];p;p=G[p].next) if (V!=fa){ if (!depth[V]){ dfs(V,u); }else if (depth[V]<depth[u]){ flag|=((depth[u]-depth[V])%2==0); } }}int n,m,K;inline int Pow(int a,int b,int P){ int ret=1; for (;b;b>>=1,a=a*a%P) if (b&1) ret=ret*a%P; return ret;}int main(){ int T,x,y; freopen("t.in","r",stdin); freopen("t.out","w",stdout); read(T); while (T--){ read(n); read(m); read(K); inum=0; cl(head); for (int i=1;i<=m;i++) read(x),read(y),add(x,y,++inum),add(y,x,++inum); int mod2,mod3; if (~K&1) mod2=0; else mod2=(m==0); if (K%3==0) mod3=0; else if (K%3==1) mod3=(m==0); else{ cl(depth); cl(fat); int cnt=0; flag=0; for (int i=1;i<=n;i++) if (!depth[i]) cnt++,dfs(i,0); if (flag) mod3=0; else mod3=Pow(2,cnt,3); } for (int i=0;i<6;i++) if (i%2==mod2 && i%3==mod3) printf("%d\n",i); } return 0;}
TC那个题是求把无向图定向成DAG的方案数
有定理,也可见这里
The number of Acyclic Orientations of an undirected graph is given by
( − 1)N⋅P(−1) , whereP is the chromatic polynomial of the given graph.
// BEGIN CUT HERE #include<conio.h>#include<sstream>// END CUT HERE #include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>#include<vector>#include<map>#include<string>#include<set>#define cl(x) memset(x,0,sizeof(x))using namespace std;typedef long long ll;const int N=105;const int M=N*N;struct edge{ int u,v,next;}G[M];int head[N],inum;inline void add(int u,int v,int p){ G[p].u=u; G[p].v=v; G[p].next=head[u]; head[u]=p;}#define V G[p].vint depth[N];int flag=0;inline void dfs(int u,int fa){ depth[u]=depth[fa]+1; for (int p=head[u];p;p=G[p].next) if (!depth[V]) dfs(V,u); else if (depth[V]<depth[u] && (depth[u]-depth[V])%2==0) flag=1;}inline int Pow(int a,int b){ int ret=1; for (;b;b>>=1,a=a*a%3) if (b&1) ret=ret*a%3; return ret;}class AcyclicOrientation{public: int count(int n, vector <int> u, vector <int> v){ cl(head); inum=0; cl(depth); flag=0; for (int i=0;i<(int)u.size();i++) add(u[i]+1,v[i]+1,++inum),add(v[i]+1,u[i]+1,++inum); int m2=(u.size()==0),m3,C=0; for (int i=1;i<=n;i++) if (!depth[i]) dfs(i,0),C++; if (flag) m3=0; else m3=Pow(2,n+C); for (int i=0;i<6;i++) if (i%2==m2 && i%3==m3) return i; } // BEGIN CUT HEREpublic: void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); }private: template <typename T> string print_array(const vector<T> &_V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = _V.begin(); iter != _V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } void test_case_0() { int Arg0 = 3; int Arr1[] = {0, 1, 0}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {2, 2, 1}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 0; verify_case(0, Arg3, count(Arg0, Arg1, Arg2)); } void test_case_1() { int Arg0 = 5; int Arr1[] = {0, 1, 2, 3}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {1, 2, 3, 4}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 4; verify_case(1, Arg3, count(Arg0, Arg1, Arg2)); } void test_case_2() { int Arg0 = 4; int Arr1[] = {0, 3}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {1, 1}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 4; verify_case(2, Arg3, count(Arg0, Arg1, Arg2)); } // END CUT HERE};// BEGIN CUT HEREint main(){ AcyclicOrientation ___test; ___test.run_test(-1); getch() ; return 0;}// END CUT HERE
阅读全文
0 0
- [色多项式] UOJ #308. 【UNR #2】UOJ拯救计划 & SRM 717 div1 AcyclicOrientation
- [组合] UOJ#308. 【UNR #2】UOJ拯救计划
- [期望DP] UOJ#311. 【UNR #2】积劳成疾
- [DP] UOJ #311. 【UNR #2】积劳成疾
- UOJ 2017NOI Round #2 T1:UOJ拯救计划(排列组合)
- [FWT] UOJ#310. 【UNR #2】黎明前的巧克力
- [FWT] UOJ #310. 【UNR #2】黎明前的巧克力
- UOJ#218 【UNR #1】火车管理
- UOJ#214. 【UNR #1】合唱队形
- [期望] UOJ #214. 【UNR #1】合唱队形
- 【UOJ 34】多项式乘法
- [UOJ#34]多项式乘法
- UOJ 34 多项式乘法
- UOJ 34 多项式乘法
- UOJ 34 多项式乘法
- uoj#34多项式乘法
- UOJ #34 多项式乘法
- UOJ #34. 多项式乘法
- Ubuntu 安装nginx 来搭建推流服务器
- 无线传感器网络(WSN)
- “撞墙死”
- Linux堆内存管理深入分析
- 友善之臂视频监控方案源码学习(6)
- [色多项式] UOJ #308. 【UNR #2】UOJ拯救计划 & SRM 717 div1 AcyclicOrientation
- 下拉菜单的手风琴效果
- Unity3d 技巧(2)-移动开发ADB 的简单使用
- 实现简单的动态代理
- 对网络传输层的理解
- HDU 2955 Robberies (01背包)
- centos系统用yum安装mysql5.7
- “找炸弹”
- 类型安全是?