Codeforces 506D Mr Kitayuta ' s colorful Graph and collection + water classification discussion + off-line pretreatment of water and water

Source: Internet
Author: User

First read all sides and ask. Categorize edges by color.

According to the color and check the set,

If this and the point in the set <= 100, then the 100*100/2 enumeration is connected.

If this and the point > 100 in the Set, then all inquiries related to these points will be checked again.

The time complexity is 100*100/2* (m/100), or m/100*q.

Limit when both methods are around 100 million, and each time also need to do in the map, but also to inquire whether Unicom, do not know why not timed out.

#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include < cstdio> #include <queue> #include <cmath> #include <stack> #include <map> #pragma comment (  linker, "/stack:1024000000") #define EPS (1e-8) #define LL long long#define ULL unsigned long long#define _ll __int64#define INF 0x3f3f3f3f#define Mod 6000007//** I/O Accelerator Interface. **/#define G (C=getchar ()) #define D isdigit (g) #define P x=x*10+c-' 0 ' #define N x=x*10+ ' 0 '-c#define pp l/=10,p#define nn l/=    10,ntemplate<class t> inline t& RD (T &x) {char C;    while (!D);    x=c-' 0 ';    while (d) p; return x;}    Template<class t> inline t& RDD (T &x) {char C;    while (g,c!= '-' &&!isdigit (c));        if (c== '-') {x= ' 0 '-G;    while (d) n;        } else {x=c-' 0 ';    while (d) p; } return x;} Inline double& RF (double &x)//scanf ("%lf", &x);    {char C; while (g,c!= '-' &&c!= '. & &!isdigit (c));        if (c== '-') if (g== '. ')            {x=0;            Double l=1;            while (d) nn;        X*=l;            } else {x= ' 0 '-C;            while (d) n;            if (c== '. ')                {Double l=1;                while (d) nn;            X*=l;    }} else if (c== '. ')        {x=0;        Double l=1;        while (d) pp;    X*=l;        } else {x=c-' 0 ';        while (d) p;        if (c== '. ')            {Double l=1;            while (d) pp;        X*=l; }} return x;} #undef nn#undef pp#undef n#undef p#undef d#undef gusing namespace std;struct qu{int u,v;} query[100010];struct N{I NT U,v,c;} Edge[100010];bool cmp (N n1,n n2) {return n1.c < n2.c;} int Fa[101000];bool mark[101000];map<pair<int,int>,int> m;vector<int> vec[101000],qu;int Find (int    x) {int f = x,t;    while (f! = Fa[f]) f = fa[f]; while (x! = fa[x]) T = Fa[x],fa[x] = F,x = t; return F;}    void Merge (int u,int v) {int fu = Find (u);    int FV = Find (v);    if (fu! = FV) {Fa[fu] = FV;    }}inline void Cal () {int i,j,k,f,v;            if (Qu.size () >) {for (j = qu.size ()-1; J >= 0;--j) {f = qu[j]; for (k = Vec[f].size ()-1; k >= 0;--k) {if (find (f) = = Find (Vec[f][k])) m[p            Air<int,int> (F,vec[f][k])]++;            }}} else {for (i = qu.size () -1;i >= 0;-i) {f = qu[i];                for (j = i-1;j >= 0;--j) {v = qu[j];            if (find (f) = = Find (v)) m[pair<int,int> (min (f,v), Max (f,v))]++;    }}} for (j = qu.size ()-1; J >= 0;--j) fa[qu[j]] = Qu[j],mark[qu[j]] = false; Qu.clear ();}    int main () {int i,j,n,m,u,v,f,k,pre;    scanf ("%d%d", &n,&m); for (i = 1; I <= m; ++i) scanf ("%d%d%d", &AMP;EDGE[I].U,&AMP;EDGE[I].V,&AMP;EDGE[I].C);    int q;    scanf ("%d", &q);        for (i = 1; I <= Q; ++i) {scanf ("%d%d", &u,&v);        if (U > V) swap (u,v);        query[i].u = u;        QUERY[I].V = v;            if (M.find (pair<int,int> (u,v)) = = M.end ()) {vec[u].push_back (v);            Vec[v].push_back (U);        M.insert (pair<pair<int,int>,int> (pair<int,int> (u,v), 0));    }} sort (edge+1,edge+m+1,cmp);    for (i = 1; I <= n; ++i) fa[i] = i;    memset (Mark,false,sizeof (Mark));    Map<pair<int,int>,int>::iterator it; for (pre = Edge[1].c,i = 1; I <= m; ++i) {if (pre = = edge[i].c) {if (mark[edge[i].u] = = Fals            e) Qu.push_back (EDGE[I].U), mark[edge[i].u] = true;            if (mark[edge[i].v] = = False) Qu.push_back (EDGE[I].V), mark[edge[i].v] = true;        Merge (EDGE[I].U,EDGE[I].V);  } else {          Pre = EDGE[I].C;            I.;        Cal ();    }} Cal ();    for (i = 1; I <= Q; ++i) {printf ("%d\n",m[pair<int,int> (QUERY[I].U,QUERY[I].V)]); } return 0;}

Codeforces 506D Mr Kitayuta ' s colorful Graph and collection + water classification discussion + off-line pretreatment of water and water

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.