zoukankan      html  css  js  c++  java
  • FJNU 1154 Fat Brother And His Love(胖哥与女神)

    FJNU 1154 Fat Brother And His Love(胖哥与女神)

    Time Limit: 2000MS   Memory Limit: 257792K

    【Description】

    【题目描述】

    As we know, fat Brother and his goddess is in a same city. The city is consist of N locations and the N locations is connected by M roads. Fat Brother has a crush on his goddess, but when he knew that the goddess want to date with other boy, he is reluctant. So he decided to stop their dating, then he go to find a single giant bomb from the warehouse. He can use this giant bomb blew up a road. Fat Brother wondered whether he can separate goddess and other boy by blowing up a road. Fat Brother gives Q questions that each query is given two numbers u and v which means the number of the location of the goddess and the boy. If fat Brother can separate goddess and other boy, output a line “Hei!Hei!Hei!” and a line integer denoting the ways to separate them. If fat Brother can't, output a line “No! I choose to go die!”.

    You can think the city which Fat Brother, goddess and boys are in is an undirected graph, and the graph is always connected in the beginning.

    If you can't understand it, you should observate the sample Input and sample Output

    众所周知,胖哥与他的女神同城。这座城市有N个地方被M条路连接。胖哥迷恋着女神,当他知晓女神要和其他男生约会时,他是拒绝的。因此他决定去阻止这一切,接着他在仓库里找了个大炸弹。他可以用这个炸弹破坏一条路。胖哥想知道是否通过破坏一条路阻隔女神与其他男生。胖哥给出Q个问题,每个问题给定两个数u和v表示女神与其男生的位置。如果胖哥可以阻隔女神与其他男生,输出一行“Hei!Hei!Hei!”与一行整数表示将他们分开方式。否则输出一行“No! I choose to go die!”。

    你可以认为胖哥所在的城市中,女神与男生在一个无向图中,并且开始时图都是连通的。

    如果你还是不明觉厉,可以看看输入输出样例。

    【Input】

    【输入】

    There are multiple test cases. The first line of input contains an integer T (T <= 25) indicating the number of test cases. For each test case:

    The first line contains three integer N, M and Q denoting there are N locations and M roads in the city. The Q denoting there are Q questions. (1 <= N <= 100000, 1 <= m <= 100000, 1 <= Q <= 100000)

    Each of the 2…M + 1 lines contains two integers u and v denoting there is a undirected road between u and v.

    Each of the M + 2 … M + 1 + Q lines contains two integer u and v denoting the fat Brother's question.

    多组测试用例。

    第一行是一个整数T(T <= 25)表示测试用例的数量。对于每个测试用例:

    第一行有三个数N, M与Q 表示这个城市有N个地方M条路。Q表示有Q个问题。(1 <= N <= 100000, 1 <= m <= 100000, 1 <= Q <= 100000)

    第2…M + 1行每行有两个整数u和v表示u与v间有一条双向的路。

    第M + 2 … M + 1 + Q行每行有两个整数u与v,表示胖哥的问题。

    【Output】

    【输出】

    For each case, output according to Title Description.

    对于每个用例,输出题目描述要求的结果。

    【Sample Input - 输入样例】

    【Sample Output - 输出样例】

    2

    9 11 19

    1 2

    1 3

    2 3

    2 4

    4 5

    4 6

    5 6

    6 7

    7 8

    7 9

    8 9

    1 2

    1 3

    2 3

    2 4

    4 5

    4 6

    5 6

    6 7

    7 8

    7 9

    8 9

    1 4

    4 7

    8 5

    3 6

    9 4

    1 6

    7 3

    2 9

    10 9 8

    1 2

    1 3

    3 4

    4 5

    3 6

    6 7

    3 8

    8 9

    8 10

    1 5

    1 2

    7 5

    9 4

    2 3

    6 2

    4 1

    3 9

    No! I choose to go die!

    No! I choose to go die!

    No! I choose to go die!

    Hei!Hei!Hei!

    1

    No! I choose to go die!

    No! I choose to go die!

    No! I choose to go die!

    Hei!Hei!Hei!

    1

    No! I choose to go die!

    No! I choose to go die!

    No! I choose to go die!

    Hei!Hei!Hei!

    1

    Hei!Hei!Hei!

    1

    Hei!Hei!Hei!

    1

    Hei!Hei!Hei!

    1

    Hei!Hei!Hei!

    1

    Hei!Hei!Hei!

    1

    Hei!Hei!Hei!

    2

    Hei!Hei!Hei!

    2

    Hei!Hei!Hei!

    3

    Hei!Hei!Hei!

    1

    Hei!Hei!Hei!

    4

    Hei!Hei!Hei!

    3

    Hei!Hei!Hei!

    2

    Hei!Hei!Hei!

    3

    Hei!Hei!Hei!

    2

    Hei!Hei!Hei!

    2

    【题解】


    2016.10.30补:此代码在

    1
    3 2 1
    1 3
    3 2
    1 3

    时存在问题,测试数据的给出方式似乎恰好回避了访问乱序

    实际中的缩点算法存在漏洞,待更新(加个数组就好了,懒癌发作,有空再补)……


    题目的大意就是在连通的无向图里面找割边的数量。

    大体步骤:①缩点(合并环)Tarjan算法 ②建立多叉树 ③查询

      Tarjan算法:百度一下(反正我也看不懂,直接看代码的)

      建立多叉树:因为环都没有了,因此图就变成一颗多叉树了

      查询:

        暴力搜索:因为目测暴力搜索会超时,所以就没去试了(懒癌发作)

        树链剖分:根据某人表示可以用,然而本渣并不能看懂……

    所以我就看着树链剖分的那幅图,自己琢磨着把树用部分转数组的方式加快

    从1开始,把其中一个分枝加入读取行的数组,其他分枝都加入到下一行的数组中去。保存每个元素第一次出现的位置,每次跳转到当前数组开头只需要O(1),晚建立的层不断向早建立的层调整,可以快速(大雾)得到公共父节点,并且在执行的时候把经过的边数累计,就可以得到割边数量了。

    【代码 C++】

      1 #include<cstdio>
      2 #include <cstring>
      3 #include <algorithm>
      4 #include <queue>
      5 #define mx 100005
      6 
      7 struct edge {
      8     int to, next;
      9 }Edge[mx], EdgeOLD[mx << 1];
     10 int Head[mx], iE, iE_OLD, ID[mx];
     11 int Stack[mx], iS = 0;
     12 bool inUS[mx];
     13 void addEdgeOLD(int u, int v) {
     14     EdgeOLD[iE_OLD].next = Head[u]; EdgeOLD[iE_OLD].to = v;
     15     Head[u] = iE_OLD++;
     16 }
     17 void addEdge(int u, int v) {
     18     Edge[iE].next = Head[u]; Edge[iE].to = v;
     19     Head[u] = iE++;
     20 }
     21 
     22 void DFS_Tarjan(int now, int anti) {//(当前节点, 反向边)
     23     ID[now] = Stack[++iS] = now;
     24     inUS[now] = 1;
     25     int u, v, i = iS;
     26     for (u = Head[now]; ~u; u = EdgeOLD[u].next) {
     27         if (u == anti) continue;
     28         v = EdgeOLD[u].to;
     29         if (!inUS[v]) DFS_Tarjan(v, u ^ 1);
     30         ID[now] = std::min(ID[now], ID[v]);
     31     }
     32     if (Stack[i] == ID[now]) {
     33         while (i <= iS) {
     34             inUS[Stack[iS]] = 0;
     35             ID[Stack[iS--]] = ID[now];
     36         }
     37     }
     38 }
     39 void markID() {
     40     iS = 0;
     41     memset(inUS, 0, sizeof(inUS));
     42     DFS_Tarjan(1, -1);
     43 }
     44 
     45 std::vector<int> listTree[mx];
     46 struct Point {
     47     int y, x;
     48 }PointAddress[mx];
     49 void BFS() {
     50     int u, v, y, x, now;
     51     bool fst = 0;
     52     std::queue<Point> q;
     53     q.push({ iS = 0, 0 });
     54     PointAddress[1] = { 0, 0 };
     55     listTree[0].push_back(1);
     56     ++inUS[1];
     57 
     58     while (!q.empty()) {
     59         y = q.front().y; x = q.front().x;
     60         now = listTree[y][x]; q.pop();
     61         fst = 0;
     62 
     63         for (u = Head[now]; ~u; u = Edge[u].next) {
     64             v = Edge[u].to;
     65             if (inUS[v]) continue;
     66             ++inUS[v];
     67             if (!fst) {//加入当前行
     68                 q.push(PointAddress[v] = { y, listTree[y].size() });
     69                 listTree[y].push_back(v); ++fst;
     70             }
     71             else {//加入新一行
     72                 listTree[++iS].push_back(now);
     73                 q.push(PointAddress[v] = { iS, listTree[iS].size() });
     74                 listTree[iS].push_back(v);
     75             }
     76         }
     77     }
     78 }
     79 void buildAddress(int n) {
     80     iE = 0;
     81     int HeadOLD[mx], i, u, v;
     82     memcpy(HeadOLD, Head, sizeof(Head));
     83     memset(Head, -1, sizeof(Head));
     84     for (i = 1; i <= n; ++i) {//建立多叉树
     85         for (u = HeadOLD[i]; ~u; u = EdgeOLD[u].next) {
     86             v = EdgeOLD[u].to;
     87             if (ID[i] < ID[v]) addEdge(ID[i], ID[v]);
     88         }
     89     }
     90 
     91     for (i = 0; i < mx; ++i) listTree[i].clear();
     92     BFS();//将各个节点加入数组
     93 }
     94 
     95 int fid(Point u, Point v) {
     96     int opt = 0, to;
     97     Point temp;
     98     while (u.y != v.y) {//未跳转到同一行时进行跳转,并记录经过边的数量。
     99         if (u.y < v.y) { temp = v; v = u; u = temp; }
    100         opt += u.x;
    101         to = listTree[u.y][0];
    102         u = PointAddress[to];
    103     }
    104     if (u.x < v.x) { u.x ^= v.x; v.x ^= u.x; u.x ^= v.x; }
    105     return opt + u.x - v.x;
    106 }
    107 
    108 int main() {
    109     int t, n, m, q, i, u, v;
    110     while (~scanf("%d", &t)) {
    111         while (t--) {
    112             memset(Head, -1, sizeof(Head)); iE_OLD = 0;
    113             scanf("%d%d%d", &n, &m, &q);
    114             for (i = 0; i < m; ++i) {
    115                 scanf("%d%d", &u, &v);
    116                 addEdgeOLD(u, v); addEdgeOLD(v, u);
    117             }
    118             markID();//对各个顶点进行再编号
    119             buildAddress(n);//建立各顶点的地址
    120             for (i = 0; i < q; ++i) {
    121                 scanf("%d%d", &u, &v);
    122                 u = ID[u]; v = ID[v];
    123                 if (u == v) puts("No! I choose to go die!");
    124                 else {
    125                     puts("Hei!Hei!Hei!");
    126                     printf("%d
    ", fid(PointAddress[u], PointAddress[v]));
    127                 }
    128             }
    129         }
    130     }
    131     return 0;
    132 }
    FJNU 1154
      1 #include<cstdio>
      2 #include <cstring>
      3 #include <algorithm>
      4 #include <queue>
      5 #define mx 100005
      6 int readInt() {
      7     int add = getchar() - '0';
      8     for (char a = getchar(); a >= '0' && a <= '9'; a = getchar()) {
      9         add = add * 10 + a - '0';
     10     }
     11     return add;
     12 }
     13 
     14 struct edge {
     15     int to, next;
     16 }Edge[mx], EdgeOLD[mx << 1];
     17 int Head[mx], iE, iE_OLD, ID[mx], HeadA[mx];
     18 int Stack[mx], iS = 0;
     19 bool inUS[mx];
     20 void addEdgeOLD(int u, int v) {
     21     EdgeOLD[iE_OLD].next = Head[u]; EdgeOLD[iE_OLD].to = v;
     22     Head[u] = iE_OLD++;
     23 }
     24 void addEdge(int u, int v) {
     25     Edge[iE].next = Head[u]; Edge[iE].to = v;
     26     Head[u] = iE++;
     27 }
     28 
     29 void DFS_Tarjan(int now, int anti) {//(当前节点, 反向边)
     30     ID[now] = Stack[++iS] = now;
     31     inUS[now] = 1;
     32     int u, v, i = iS;
     33     for (u = Head[now]; ~u; u = EdgeOLD[u].next) {
     34         if (u == anti) continue;
     35         v = EdgeOLD[u].to;
     36         if (!inUS[v]) DFS_Tarjan(v, u ^ 1);
     37         ID[now] = std::min(ID[now], ID[v]);
     38     }
     39     if (Stack[i] == ID[now]) {
     40         while (i <= iS) {
     41             inUS[Stack[iS]] = 0;
     42             ID[Stack[iS--]] = ID[now];
     43         }
     44     }
     45 }
     46 void markID() {
     47     iS = 0;
     48     memset(inUS, 0, sizeof(inUS));
     49     DFS_Tarjan(1, -1);
     50 }
     51 
     52 struct Point {
     53     int y, x, data;
     54 }PointAddress[mx];
     55 void BFS() {
     56     int u, v, y, x, now, HeadASize[mx];
     57     memset(HeadASize, 0, sizeof(HeadASize));
     58     bool fst = 0;
     59     std::queue<Point> q;
     60     q.push({ iS = 0, 0, 1 });
     61     PointAddress[1] = { 0, 0, 1 };
     62     HeadA[0] = 1; ++HeadASize[0];
     63     ++inUS[1];
     64 
     65     while (!q.empty()) {
     66         y = q.front().y; x = q.front().x; now = q.front().data;
     67         q.pop();
     68         fst = 1;
     69 
     70         for (u = Head[now]; ~u; u = Edge[u].next) {
     71             v = Edge[u].to;
     72             if (inUS[v]) continue;
     73             ++inUS[v];
     74             if (fst) {//加入当前行
     75                 q.push(PointAddress[v] = { y, HeadASize[y]++, v });
     76                 fst = 0;
     77             }
     78             else {//加入新一行
     79                 HeadA[++iS] = now; ++HeadASize[iS];
     80                 q.push(PointAddress[v] = { iS, HeadASize[iS]++, v });
     81             }
     82         }
     83     }
     84 }
     85 void buildAddress(int n) {
     86     iE = 0;
     87     int HeadOLD[mx], i, u, v;
     88     memcpy(HeadOLD, Head, sizeof(Head));
     89     memset(Head, -1, sizeof(Head));
     90     for (i = 1; i <= n; ++i) {//建立多叉树
     91         for (u = HeadOLD[i]; ~u; u = EdgeOLD[u].next) {
     92             v = EdgeOLD[u].to;
     93             if (ID[i] < ID[v]) addEdge(ID[i], ID[v]);
     94         }
     95     }
     96 
     97     BFS();//将各个节点加入数组
     98 }
     99 int fid(Point u, Point v) {
    100     int opt = 0;
    101     Point temp;
    102     while (u.y != v.y) {//未跳转到同一行时进行跳转,并记录经过边的数量。
    103         if (u.y < v.y) { temp = v; v = u; u = temp; }
    104         opt += u.x;
    105         u = PointAddress[HeadA[u.y]];
    106     }
    107     if (u.x < v.x) { u.x ^= v.x; v.x ^= u.x; u.x ^= v.x; }
    108     return opt + u.x - v.x;
    109 }
    110 int main() {
    111     int t, n, m, q, i, u, v;
    112     while (t = readInt(), t>0) {
    113         while (t--) {
    114             memset(Head, -1, sizeof(Head)); iE_OLD = 0;
    115             n = readInt(); m = readInt(); q = readInt();
    116             for (i = 0; i < m; ++i) {
    117                 u = readInt(); v = readInt();
    118                 addEdgeOLD(u, v); addEdgeOLD(v, u);
    119             }
    120             markID();//对各个顶点进行再编号
    121             buildAddress(n);//建立各顶点的地址
    122             for (i = 0; i < q; ++i) {
    123                 u = ID[readInt()]; v = ID[readInt()];
    124                 if (u == v) puts("No! I choose to go die!");
    125                 else {
    126                     puts("Hei!Hei!Hei!");
    127                     printf("%d
    ", fid(PointAddress[u], PointAddress[v]));
    128                 }
    129             }
    130         }
    131     }
    132     return 0;
    133 }

    【后记】

    由于不同平台对栈内存的分配不同,在windows默认1MB的情况下某些数据会出现爆栈的情况(OJ上是linux系统),自己可以提高栈内存的大小。

  • 相关阅读:
    [Oracle工程师手记]Linux环境中,反复调用SQLPLUS 执行SQL语句的例子
    [Oracle工程师手记] 记一次 transport lag 的解析
    [Oracle工程师手记] V$ARCHIVE_GAP中的 GAP 何时产生?
    [Oracle 工程师手记] nologging 操作的优先级
    [Oracle 工程师手记] ORA-16642: DB_UNIQUE_NAME mismatch 的解决过程
    [Oracle 工程师手记] 如何构造数据库的 log on trigger
    Groovy调用MD5加密
    Jenkins Pipeline调用 httpRequest插件提交Http请求
    Python调用Jenkins接口批准/拒绝Pipeline流程
    Access 的top和order by 的问题
  • 原文地址:https://www.cnblogs.com/Simon-X/p/5592663.html
Copyright © 2011-2022 走看看