zoukankan      html  css  js  c++  java
  • ZOJ 3261 Connections in Galaxy War

    题目链接http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3563

    解题思路:原来并查集也可以离线。。。离线保存然后从后往前,这样每次destroy就成了unite了,那么就是带权并查集。

    每个集合的根保存整个集合的最大值的以及最大值的最小下标,每次unite的时候更新即可。

    注意输入的时候a b的大小。

    代码:

     1 const int maxn =  1e4 + 5;
     2 vector<PII> que, con;
     3 vector<int> desp;
     4 set<PII> dess;
     5 int fa[maxn], val[maxn], ind[maxn], p[maxn];
     6 int n, m, q;
     7 stack<int> anss;
     8 
     9 void init(){
    10     while(!anss.empty()) anss.pop();
    11     for(int i = 0; i <= n; i++) ind[i] = fa[i] = i;
    12     que.clear(); desp.clear();
    13     con.clear(); dess.clear();
    14 }
    15 int find(int x){
    16     if(x == fa[x]) return x;
    17     return fa[x] = find(fa[x]);
    18 }
    19 void unite(int x, int y){
    20     x = find(x);
    21     y = find(y);
    22     if(x != y){
    23         fa[x] = y;
    24         if(val[x] >= val[y]){
    25             if(val[x] == val[y] && ind[x] < ind[y]) {
    26                 ind[y] = ind[x];
    27             }
    28             if(val[x] > val[y]) {
    29                 ind[y] = ind[x];
    30                 val[y] = val[x];
    31             }
    32         }
    33     }
    34 }
    35 
    36 int main(){
    37     bool first = true;
    38     while(scanf("%d", &n) != EOF){
    39         if(!first) puts("");
    40         first = false;
    41         init();
    42         for(int i = 0; i < n; i++){
    43             scanf("%d", &p[i]);
    44             val[i] = p[i];
    45         }
    46         scanf("%d", &m);
    47         for(int i = 0; i < m; i++){
    48             int tma, tmb;
    49             scanf("%d %d", &tma, &tmb);
    50             if(tma >= tmb) swap(tma, tmb);
    51             con.push_back(PII(tma, tmb));
    52         }
    53 
    54         scanf("%d", &q);
    55         for(int i = 0; i < q; i++){
    56             int tma, tmb;
    57             char str[10];
    58             scanf("%s", str);
    59             if(str[0] == 'q'){
    60                 scanf("%d", &tma);
    61                 que.push_back(PII(tma, -1));
    62                 desp.push_back(0);
    63             }
    64             else{
    65                 scanf("%d %d", &tma, &tmb);
    66                 if(tma >= tmb) swap(tma, tmb);
    67                 que.push_back(PII(tma, tmb));
    68                 desp.push_back(1);
    69                 dess.insert(PII(tma, tmb));
    70             }
    71         }
    72         for(int i = 0; i < con.size(); i++){
    73             if(dess.find(con[i]) == dess.end())
    74                 unite(con[i].first, con[i].second);
    75         }
    76         for(int i = que.size() - 1; i >= 0; i--){
    77             if(desp[i]){
    78                 unite(que[i].first, que[i].second);
    79             }
    80             else{
    81                 int tma = que[i].first;
    82                 int tmra = find(tma);
    83                 int tmans = (val[tmra] <= p[tma]? -1: ind[tmra]);
    84                 anss.push(tmans);
    85             }
    86         }
    87         while(!anss.empty()){
    88             printf("%d
    ", anss.top());
    89             anss.pop();
    90         }
    91     }
    92 }

    题目:

    Connections in Galaxy War

    Time Limit: 3 Seconds      Memory Limit: 32768 KB

    In order to strengthen the defense ability, many stars in galaxy allied together and built many bidirectional tunnels to exchange messages. However, when the Galaxy War began, some tunnels were destroyed by the monsters from another dimension. Then many problems were raised when some of the stars wanted to seek help from the others.

    In the galaxy, the stars are numbered from 0 to N-1 and their power was marked by a non-negative integer pi. When the star A wanted to seek help, it would send the message to the star with the largest power which was connected with star A directly or indirectly. In addition, this star should be more powerful than the star A. If there were more than one star which had the same largest power, then the one with the smallest serial number was chosen. And therefore, sometimes star A couldn't find such star for help.

    Given the information of the war and the queries about some particular stars, for each query, please find out whether this star could seek another star for help and which star should be chosen.

    Input

    There are no more than 20 cases. Process to the end of file.

    For each cases, the first line contains an integer N (1 <= N <= 10000), which is the number of stars. The second line contains N integers p0p1, ... , pn-1 (0 <= pi <= 1000000000), representing the power of the i-th star. Then the third line is a single integer M (0 <= M <= 20000), that is the number of tunnels built before the war. Then M lines follows. Each line has two integers ab (0 <= ab <= N - 1, a != b), which means star a and star b has a connection tunnel. It's guaranteed that each connection will only be described once.

    In the (M + 2)-th line is an integer Q (0 <= Q <= 50000) which is the number of the information and queries. In the following Q lines, each line will be written in one of next two formats.

    "destroy a b" - the connection between star a and star b was destroyed by the monsters. It's guaranteed that the connection between star a and star b was available before the monsters' attack.

    "query a" - star a wanted to know which star it should turn to for help

    There is a blank line between consecutive cases.

    Output

    For each query in the input, if there is no star that star a can turn to for help, then output "-1"; otherwise, output the serial number of the chosen star.

    Print a blank line between consecutive cases.

    Sample Input

    2
    10 20
    1
    0 1
    5
    query 0
    query 1
    destroy 0 1
    query 0
    query 1
    

    Sample Output

    1
    -1
    -1
    -1
    

    Author: MO, Luyi
    Source: ZOJ Monthly, November 2009

  • 相关阅读:
    一个购物网站的思路设计分享
    B/S和C/S的区别(转)
    TreeSet
    计算出给你一个随机乱敲的一个字符串最多的一个
    JavaScript来实现打开链接页面(转载)
    js小数计算小数点后显示多位小数(转)
    java中使用 正则 抓取邮箱
    浅谈 正则表达式
    jQuery中each()、find()、filter()等节点操作方法
    Xcode插件VVDocumenter Alcatraz KSImageNamed等安装
  • 原文地址:https://www.cnblogs.com/bolderic/p/7299188.html
Copyright © 2011-2022 走看看