zoukankan      html  css  js  c++  java
  • DFS/并查集 Codeforces Round #286 (Div. 2) B

    题目传送门

     1 /*
     2     题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条
     3     DFS:暴力每个颜色,以u走到v为结束标志,累加条数
     4     注意:无向图
     5 */
     6 #include <cstdio>
     7 #include <iostream>
     8 #include <algorithm>
     9 #include <cstring>
    10 #include <string>
    11 #include <vector>
    12 using namespace std;
    13 
    14 const int MAXN = 1e2 + 10;
    15 const int INF = 0x3f3f3f3f;
    16 int n, m;
    17 bool vis[MAXN];
    18 vector<pair<int, int> > V[MAXN];
    19 
    20 bool DFS(int u, int v, int c)
    21 {
    22     vis[u] = true;
    23     if (u == v)    return true;
    24     for (int i=0; i<V[u].size (); ++i)
    25     {
    26         pair<int, int>  p = V[u][i];
    27         if (p.second == c && !vis[p.first])
    28         {
    29             if (DFS (p.first, v, c))    return true;
    30         }
    31     }
    32 
    33     return false;
    34 }
    35 
    36 int main(void)        //Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph
    37 {
    38     //freopen ("B.in", "r", stdin);
    39 
    40     while (scanf ("%d%d", &n, &m) == 2)
    41     {
    42         for (int i=1; i<=m; ++i)
    43         {
    44             int u, v, c;
    45             scanf ("%d%d%d", &u, &v, &c);
    46             V[u].push_back (make_pair (v, c));
    47             V[v].push_back (make_pair (u, c));
    48         }
    49         int q;    scanf ("%d", &q);
    50         while (q--)
    51         {
    52             int u, v;    scanf ("%d%d", &u, &v);
    53             int ans = 0;
    54             for (int i=1; i<=m; ++i)
    55             {
    56                 memset (vis, false, sizeof (vis));
    57                 if (DFS (u, v, i))    ans++;
    58             }
    59             printf ("%d
    ", ans);
    60         }
    61     }
    62 
    63     return 0;
    64 }
     1 /*
     2     并查集:开在结构体的并查集,进行如下的两个操作
     3     uf[c].Union (u, v);    uf[i].same (u, v)
     4 */
     5 #include <cstdio>
     6 #include <iostream>
     7 #include <algorithm>
     8 #include <cstring>
     9 #include <vector>
    10 using namespace std;
    11 
    12 const int MAXN = 1e2 + 10;
    13 const int INF = 0x3f3f3f3f;
    14 struct UF
    15 {
    16     int rt[MAXN];
    17     void init(void)    {memset (rt, -1, sizeof (rt));}
    18 
    19     int Find(int x)    {return (rt[x] == -1) ? x : rt[x] = Find (rt[x]);}
    20 
    21     void Union(int x, int y)
    22     {
    23         int tx = Find (x);
    24         int ty = Find (y);
    25         if (tx > ty)    rt[ty] = tx;
    26         else if (tx < ty)    rt[tx] = ty;
    27     }
    28 
    29     bool same(int x, int y)
    30     {
    31         return (Find (x) == Find (y));
    32     }
    33 }uf[MAXN];
    34 int n, m;
    35 
    36 int main(void)        //Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph
    37 {
    38     //freopen ("B.in", "r", stdin);
    39 
    40     while (scanf ("%d%d", &n, &m) == 2)
    41     {
    42         for (int i=1; i<=m; ++i)    uf[i].init ();
    43         for (int i=1; i<=m; ++i)
    44         {
    45             int u, v, c;
    46             scanf ("%d%d%d", &u, &v, &c);
    47             uf[c].Union (u, v);
    48         }
    49         int q;    scanf ("%d", &q);
    50         while (q--)
    51         {
    52             int u, v;    scanf ("%d%d", &u, &v);
    53             int ans = 0;
    54             for (int i=1; i<=m; ++i)
    55             {
    56                 if (uf[i].same (u, v))    ans++;
    57             }
    58             printf ("%d
    ", ans);
    59         }
    60     }
    61 
    62     return 0;
    63 }
    并查集
    编译人生,运行世界!
  • 相关阅读:
    Windows 7 下安装 docker 应用容器引擎
    jmeter压力测试
    1分钟为Win10瘦身!把吃掉的硬盘找回来
    关于IIS应用程序池的默认参数设置解决
    IIS将应用程序池配置为在计划时间执行回收 (IIS 7)
    什么是IIS应用程序池
    WinCE知识介绍
    odoo12 通过一个字段控制另一个Many2one字段的domain
    odoo12 数据库过期问题
    odoo12
  • 原文地址:https://www.cnblogs.com/Running-Time/p/4512405.html
Copyright © 2011-2022 走看看