• POJ1984 Navigation Nightmare


        原题传送:http://poj.org/problem?id=1984

        并查集。

        首先,以为k个询问中的I是乱序的,直接被吓傻了,后来看讨论版才发现I是升序的……

        这道题让我对并查集有一个新的认识:根的代表性是非常强的!并查集里如果某个节点的改动会影响到整个并查集的所有节点,那么,在union_set的时候只需要改动根节点就可以了,当然,在find_set函数里要对所有节点进行更新(这相当于一种延时标记)。我们知道,find_set函数走了两条路,一条是前往根的路,一条是从跟返回的路,那么,如果发现根已经被改动,必须在从根返回的路上更新经过的所有节点。这在find_set函数里是可以实现的。

    View Code
     1 #include <stdio.h>
     2 #include <math.h>
     3 #include <stdlib.h>
     4 #define N 40005
     5 
     6 int f[N], cx[N], cy[N];
     7 int f1[N], f2[N], len[N];
     8 char dr[N];
     9 int find(int x)
    10 {
    11     int tmp;
    12     if(x != f[x])
    13     {
    14         tmp = f[x];
    15         f[x] = find(f[x]);
    16         cx[x] = cx[x] + cx[tmp];  // 从根回来的路上更新子节点
    17         cy[x] = cy[x] + cy[tmp];
    18     }
    19     return f[x];
    20 }
    21 
    22 void make_set(int n)
    23 {
    24     for(int i = 0; i <= n; i ++)
    25     {
    26         f[i] = i;
    27         cx[i] = cy[i] = 0;
    28     }
    29 }
    30 
    31 void union_set(int j)
    32 {
    33     int x = find(f1[j]);
    34     int y = find(f2[j]);
    35     f[y] = x;   // 随意合并
    36     cx[y] = cx[x] + cx[f1[j]] - cx[f2[j]];  // 暂且只对根进行偏移
    37     cy[y] = cy[x] + cy[f1[j]] - cy[f2[j]];
    38     switch(dr[j])
    39     {
    40         case 'W':
    41             cx[y] -= len[j];
    42             break;
    43         case 'E':
    44             cx[y] += len[j];
    45             break;
    46         case 'S':
    47             cy[y] -= len[j];
    48             break;
    49         case 'N':
    50             cy[y] += len[j];
    51             break;
    52     }
    53 }
    54 
    55 int main()
    56 {
    57     int n, m, i, j, k, q, a, b, c, x, y;
    58     while(scanf("%d%d", &n, &m) != EOF)
    59     {
    60         make_set(n);
    61         for(i = 0; i < m; i ++)
    62             scanf("%d%d%d %c", &f1[i], &f2[i], &len[i], &dr[i]);
    63         
    64         scanf("%d", &q);
    65         for(k = i = 0; i < q; i ++)
    66         {
    67             scanf("%d%d%d", &a, &b, &c);
    68             
    69             for(j = k; j < c; j ++)
    70                 union_set(j);
    71             k = c;
    72             x = find(a);
    73             y = find(b);
    74             if(x != y)
    75                 printf("-1\n");
    76             else
    77                 printf("%d\n", abs(cx[a] - cx[b]) + abs(cy[a] - cy[b]));
    78         }
    79     }
    80     return 0;
    81 }
  • 相关阅读:
    STL 清除模板容器 clear.h
    建立ORACLE10G DATA GUARD---&gt;Physical Standby
    WWDC 2014 Session 205/217 Extension 注意事项
    Android 布局管理器
    软件测试的基本方法(五岁以下儿童)单元测试
    HDU 4896 Minimal Spanning Tree(矩阵高速功率)
    泛泰A860(高通公司8064 cpu 1080p) 拂4.4中国民营recovery TWRP2.7.1.2文本(通过刷第三版)
    Android L SDK -- 一些有趣的新功能
    Jquery在线咨询地址
    项目开发录制两个
  • 原文地址:https://www.cnblogs.com/huangfeihome/p/2675123.html
Copyright © 2020-2023  润新知