Codeforces Round #286 (Div. 1) D. Mr Kitayuta's colorful Graph (and find STL)

Source: Internet
Author: User
Tags printf

Test Instructions Link
Test instructions: Give a graph that tells you the color of the edges between each point (with a heavy edge) and asks you how many sides are connected between any two points.
Solution: and set Processing, F[x][c]: Represents the x link and the color is C's vertex y
AAA: Posture is not timed out, the correct posture is unordered_map, and the violent traversal of the time to cycle small

#include <bits/stdc++.h> using namespace std; #define LL Long Long #define PB push_back #define X first #define Y second #define CL (b) memset (A,b,sizeof (a)) typedef p
Air<int,int> P;
const int maxn=100005;
Const LL inf=1<<27;
Const LL MOD=1E9+7;

Unordered_map<int,int> F[MAXN],ANS[MAXN]; int getf (int x,int c) {return x==f[x][c]?x:f[x][c]=getf (f[x][c],c);} void Union (int c,int x,int y) {if (f[x].count
    (c) ==0) f[x][c]=x;
    if (F[y].count (c) ==0) f[y][c]=y;
    X=GETF (F[X][C],C);
    Y=GETF (F[Y][C],C);
    if (x==y) return;
    if (x>y) swap (x, y);
F[x][c]=y;
    } int main () {int n,m,q;
    scanf ("%d%d", &n,&m);
        for (int i=0;i<m;i++) {int u,v,c;
        scanf ("%d%d%d", &u,&v,&c);
        if (u>v) swap (U,V);
    Union (C,U,V);
    } scanf ("%d", &q);
        while (q--) {int x,y;scanf ("%d%d", &x,&y);
        if (F[x].size () >f[y].size ()) swap (x, y);///optimization is important int ans=0; if (Ans[x].count (y)) {printf ("%d\n", Ans[x][y]); continue;}
            for (auto K:f[x]) {if (F[y].count (k.x) ==0) continue;
        if (GETF (f[x][k.x],k.x) ==getf (f[y][k.x],k.x)) ans++;
        } Ans[x][y]=ans;
    printf ("%d\n", Ans);
} 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.