Combination uoj#308. "Unr #2" Uoj Rescue plan __ Math related

Source: Internet
Author: User
the

Given a non-direction graph of N-point M-edges, all vertices of the graph are dyed so that the color of the ends is different for any one edge. There are k colors available.
The value of the 6 of the scheme of the stain is obtained. Solving

It's a simple question, but I didn't think of it when I did it ... I'm a good cook ...
is a classic problem that looks like NPC, and then only needs to output the answer%6 to make the problem specialized.
What's so special about it.
We consider the answer this way:
Ans=∑i=1ka (k,i) ∗[just use the I color scheme number] ans=\sum_{i=1}^k a (k,i) *[just use the number of colors I have.
We know the permutation number A (k,i) = (k−i+1) ∗ (k−i+2) ∗ ... ∗k A (k,i) = (k-i+1) * (k-i+2) *...*k
I believe you have learned in primary school: Any two consecutive integer products must have 2 this divisor, any three consecutive integer product must have 3 this divisor.
This is simple, when i≥3 i\ge 3 o'clock, A (k,i)%6=0, A (k,i) \% 6=0, do not care.
So we just need to consider 1 colors and 2 colors.
And then just do a little messing around.
Pay attention to the special situation of m=0 m=0.

#include <cstdio> #include <cstring> #include <algorithm> typedef long LL;
using namespace Std;
const int maxn=1000005,maxe=4000005;
int q,n,m,k,c[maxn],fa[maxn],res,cnt;
BOOL VIS[MAXN];
int Fir[maxn],nxt[maxe],son[maxe],tot; void Add (int x,int y) {son[++tot]=y; nxt[tot]=fir[x]; Fir[x]=tot} int GETFA (int x) {return FA[X]==X?X:FA[X]=GETFA (FA [x]);
    } void Merge (int x,int y) {X=GETFA (x); Y=getfa (y);
if (x!=y) fa[x]=y;
    } void Dfs (int x,int k) {vis[x]=true; c[x]=k;
                   for (int j=fir[x];j;j=nxt[j]) {if (!vis[son[j]]) DFS (SON[J],K^1);
    else if (c[son[j]]!= (k^1)) res=0;
    int main () {freopen ("uoj308.in", "R", stdin);
    Freopen ("Uoj308.out", "w", stdout);
    scanf ("%d", &q);       
        while (q--) {scanf ("%d%d%d", &n,&m,&k);
        for (int i=1;i<=n;i++) fa[i]=i; memset (FIR) (fir,0,sizeof);
        tot=0;
            for (int i=1;i<=m;i++) {int x,y; scanf ("%d%d", &x,&y); AddX,y); Add (y,x);
        Merge (X,y);
            } if (m==0) {Res=1; for (int i=1;i<=n-1;i++) res= (res*2)%6; res= (res-1+6)%6;
        printf ("%d\n", (k%6+ (k%6) * (K-1)%6*res%6)%6);
            else{for (int i=1;i<=n;i++) c[i]=-1; 
            memset (Vis) (vis,0,sizeof); Res=1;
            cnt=0;
            for (int i=1;i<=n;i++) if (!vis[i]) DFS (i,0), cnt+=1;
            for (int i=1;i<=cnt-1;i++) res= (res*2)%6;       
        printf ("%d\n", res* (k%6) * (K-1)%6);
} return 0;  }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.