• ECNUOJ 2575 Separate Connections


    Separate Connections

    Time Limit:5000MS Memory Limit:65536KB
    Total Submit:421 Accepted:41

    Description 

    Partychen are analyzing a communications network with at most 18 nodes. Character in a matrix i,j (i,j both 0-based,as matrix[i][j]) denotes whether nodes i and j can communicate ('Y' for yes, 'N' for no). Assuming a node cannot communicate with two nodes at once, return the maximum number of nodes that can communicate simultaneously. If node i is communicating with node j then node j is communicating with node i.

    Input 

    The first line of input gives the number of cases, N(1 ≤ N ≤ 100). N test cases follow.
    In each test case,the first line is the number of nodes M(1 ≤ M ≤ 18),then there are a grid by M*M describled the matrix.

    Output 

    For each test case , output the maximum number of nodes that can communicate simultaneously

    Sample Input 

    2
    5
    NYYYY
    YNNNN
    YNNNN
    YNNNN
    YNNNN
    5
    NYYYY
    YNNNN
    YNNNY
    YNNNY
    YNYYN

    Sample Output 

    2
    4
    Hint
    The first test case: 
    All communications must occur with node 0. Since node 0 can only communicate with 1 node at a time, the output value is 2.
    The second test case: 
    In this setup, we can let node 0 communicate with node 1, and node 3 communicate with node 4.

    Source

    解题:逼格很高啊!带花树模板题,可惜本人不会敲,只好找了份模板

      1 #include <cstdio>
      2 #include <cstring>
      3 #include <iostream>
      4 #include <queue>
      5 using namespace std;
      6 const int N = 250;
      7 int belong[N];
      8 int findb(int x) {
      9     return belong[x] == x ? x : belong[x] = findb(belong[x]);
     10 }
     11 void unit(int a, int b) {
     12     a = findb(a);
     13     b = findb(b);
     14     if (a != b) belong[a] = b;
     15 }
     16 
     17 int n, match[N];
     18 vector<int> e[N];
     19 int Q[N], rear;
     20 int nxt[N], mark[N], vis[N];
     21 int LCA(int x, int y) {
     22     static int t = 0;
     23     t++;
     24     while (true) {
     25         if (x != -1) {
     26             x = findb(x);
     27             if (vis[x] == t) return x;
     28             vis[x] = t;
     29             if (match[x] != -1) x = nxt[match[x]];
     30             else x = -1;
     31         }
     32         swap(x, y);
     33     }
     34 }
     35 
     36 void group(int a, int p) {
     37     while (a != p) {
     38         int b = match[a], c = nxt[b];
     39         if (findb(c) != p) nxt[c] = b;
     40         if (mark[b] == 2) mark[Q[rear++] = b] = 1;
     41         if (mark[c] == 2) mark[Q[rear++] = c] = 1;
     42 
     43         unit(a, b);
     44         unit(b, c);
     45         a = c;
     46     }
     47 }
     48 
     49 // 增广
     50 void aug(int s) {
     51     for (int i = 0; i < n; i++) // 每个阶段都要重新标记
     52         nxt[i] = -1, belong[i] = i, mark[i] = 0, vis[i] = -1;
     53     mark[s] = 1;
     54     Q[0] = s;
     55     rear = 1;
     56     for (int front = 0; match[s] == -1 && front < rear; front++) {
     57         int x = Q[front]; // 队列Q中的点都是S型的
     58         for (int i = 0; i < (int)e[x].size(); i++) {
     59             int y = e[x][i];
     60             if (match[x] == y) continue; // x与y已匹配,忽略
     61             if (findb(x) == findb(y)) continue; // x与y同在一朵花,忽略
     62             if (mark[y] == 2) continue; // y是T型点,忽略
     63             if (mark[y] == 1) { // y是S型点,奇环缩点
     64                 int r = LCA(x, y); // r为从i和j到s的路径上的第一个公共节点
     65                 if (findb(x) != r) nxt[x] = y; // r和x不在同一个花朵,nxt标记花朵内路径
     66                 if (findb(y) != r) nxt[y] = x; // r和y不在同一个花朵,nxt标记花朵内路径
     67 
     68                 // 将整个r -- x - y --- r的奇环缩成点,r作为这个环的标记节点,相当于论文中的超级节点
     69                 group(x, r); // 缩路径r --- x为点
     70                 group(y, r); // 缩路径r --- y为点
     71             } else if (match[y] == -1) { // y自由,可以增广,R12规则处理
     72                 nxt[y] = x;
     73                 for (int u = y; u != -1; ) { // 交叉链取反
     74                     int v = nxt[u];
     75                     int mv = match[v];
     76                     match[v] = u, match[u] = v;
     77                     u = mv;
     78                 }
     79                 break; // 搜索成功,退出循环将进入下一阶段
     80             } else { // 当前搜索的交叉链+y+match[y]形成新的交叉链,将match[y]加入队列作为待搜节点
     81                 nxt[y] = x;
     82                 mark[Q[rear++] = match[y]] = 1; // match[y]也是S型的
     83                 mark[y] = 2; // y标记成T型
     84             }
     85         }
     86     }
     87 }
     88 
     89 bool g[N][N];
     90 char mp[N][N];
     91 int main() {
     92     int kase;
     93     scanf("%d",&kase);
     94     while(kase--) {
     95         scanf("%d", &n);
     96         for (int i = 0; i < n; i++)
     97             for (int j = 0; j < n; j++) g[i][j] = false;
     98         for(int i = 0; i < n; ++i) e[i].clear();
     99         for(int i = 0; i < n; ++i){
    100             scanf("%s",mp[i]);
    101             for(int j = 0; j < n; ++j){
    102                 if(mp[i][j] == 'Y'){
    103                     e[i].push_back(j);
    104                     e[j].push_back(i);
    105                     g[i][j] = g[j][i] = true;
    106                 }
    107             }
    108         }
    109         for (int i = 0; i < n; i++) match[i] = -1;
    110         for (int i = 0; i < n; i++) if (match[i] == -1) aug(i);
    111         int tot = 0;
    112         for (int i = 0; i < n; i++) if (match[i] != -1) tot++;
    113         printf("%d
    ", tot);
    114     }
    115     return 0;
    116 }
    View Code
  • 相关阅读:
    linux sed 命令,sed -i
    linux子系统的初始化_subsys_initcall()
    jsp动作之 getProperty
    jsp动作之 setProperty
    eclipse jsp:useBean搞死人了。
    JSP中scope属性 scope属性决定了JavaBean对象存在的范围
    [转]mysql日常工作手记
    [转]mysql-mmm集群(多实例)
    Lua脚本语言入门学习其应用教程
    15分钟入门lua
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4630447.html
Copyright © 2020-2023  润新知