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

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

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int N = ;
int belong[N];
int findb(int x) {
return belong[x] == x ? x : belong[x] = findb(belong[x]);
}
void unit(int a, int b) {
a = findb(a);
b = findb(b);
if (a != b) belong[a] = b;
} int n, match[N];
vector<int> e[N];
int Q[N], rear;
int nxt[N], mark[N], vis[N];
int LCA(int x, int y) {
static int t = ;
t++;
while (true) {
if (x != -) {
x = findb(x);
if (vis[x] == t) return x;
vis[x] = t;
if (match[x] != -) x = nxt[match[x]];
else x = -;
}
swap(x, y);
}
} void group(int a, int p) {
while (a != p) {
int b = match[a], c = nxt[b];
if (findb(c) != p) nxt[c] = b;
if (mark[b] == ) mark[Q[rear++] = b] = ;
if (mark[c] == ) mark[Q[rear++] = c] = ; unit(a, b);
unit(b, c);
a = c;
}
} // 增广
void aug(int s) {
for (int i = ; i < n; i++) // 每个阶段都要重新标记
nxt[i] = -, belong[i] = i, mark[i] = , vis[i] = -;
mark[s] = ;
Q[] = s;
rear = ;
for (int front = ; match[s] == - && front < rear; front++) {
int x = Q[front]; // 队列Q中的点都是S型的
for (int i = ; i < (int)e[x].size(); i++) {
int y = e[x][i];
if (match[x] == y) continue; // x与y已匹配,忽略
if (findb(x) == findb(y)) continue; // x与y同在一朵花,忽略
if (mark[y] == ) continue; // y是T型点,忽略
if (mark[y] == ) { // y是S型点,奇环缩点
int r = LCA(x, y); // r为从i和j到s的路径上的第一个公共节点
if (findb(x) != r) nxt[x] = y; // r和x不在同一个花朵,nxt标记花朵内路径
if (findb(y) != r) nxt[y] = x; // r和y不在同一个花朵,nxt标记花朵内路径 // 将整个r -- x - y --- r的奇环缩成点,r作为这个环的标记节点,相当于论文中的超级节点
group(x, r); // 缩路径r --- x为点
group(y, r); // 缩路径r --- y为点
} else if (match[y] == -) { // y自由,可以增广,R12规则处理
nxt[y] = x;
for (int u = y; u != -; ) { // 交叉链取反
int v = nxt[u];
int mv = match[v];
match[v] = u, match[u] = v;
u = mv;
}
break; // 搜索成功,退出循环将进入下一阶段
} else { // 当前搜索的交叉链+y+match[y]形成新的交叉链,将match[y]加入队列作为待搜节点
nxt[y] = x;
mark[Q[rear++] = match[y]] = ; // match[y]也是S型的
mark[y] = ; // y标记成T型
}
}
}
} bool g[N][N];
char mp[N][N];
int main() {
int kase;
scanf("%d",&kase);
while(kase--) {
scanf("%d", &n);
for (int i = ; i < n; i++)
for (int j = ; j < n; j++) g[i][j] = false;
for(int i = ; i < n; ++i) e[i].clear();
for(int i = ; i < n; ++i){
scanf("%s",mp[i]);
for(int j = ; j < n; ++j){
if(mp[i][j] == 'Y'){
e[i].push_back(j);
e[j].push_back(i);
g[i][j] = g[j][i] = true;
}
}
}
for (int i = ; i < n; i++) match[i] = -;
for (int i = ; i < n; i++) if (match[i] == -) aug(i);
int tot = ;
for (int i = ; i < n; i++) if (match[i] != -) tot++;
printf("%d\n", tot);
}
return ;
}

ECNUOJ 2575 Separate Connections的更多相关文章

  1. Java性能提示(全)

    http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLi ...

  2. RFC 2616

    Network Working Group R. Fielding Request for Comments: 2616 UC Irvine Obsoletes: 2068 J. Gettys Cat ...

  3. (转) s-video vs. composite video vs. component video 几种视频格式详细说明和比较

    之前对着几种视频格式认识不是很清晰,所以看数据手册的时候,看的也是稀里糊涂的. 因为项目中需要用到cvbs做视频输入,在元器件选型上,看到tw2867的数据手册上,有这么一句话: The TW2867 ...

  4. Android USB Connections Explained: MTP, PTP, and USB Mass Storage

    Android USB Connections Explained: MTP, PTP, and USB Mass Storage Older Android devices support USB ...

  5. The threads in the thread pool will process the requests on the connections concurrently.

    https://docs.oracle.com/javase/tutorial/essential/concurrency/pools.html Most of the executor implem ...

  6. 解决mysql too many connections的问题

    由于公司服务器上的创建的项目太多,随之创建的数据库不断地增加,在用navicat链接某一个项目的数据库时会出现too many connections ,从而导致项目连接数据库异常,致使启动失败. 为 ...

  7. Data source rejected establishment of connection, message from server: "Too many connections"解决办法

    异常名称 //数据源拒绝从服务器建立连接.消息:"连接太多" com.MySQL.jdbc.exceptions.jdbc4.MySQLNonTransientConnection ...

  8. Configure Security Settings for Remote Desktop(RDP) Services Connections

    catalogue . Configure Server Authentication and Encryption Levels . Configure Network Level Authenti ...

  9. 问题解决:psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

    错误提示: psql: could not connect to server: No such file or directory Is the server running locally and ...

随机推荐

  1. 转Hibernate继承

    hibernate继承映射 以下测试是在mysql中进行的. 1.单表方式 Animal.java @Entity @Inheritance(strategy=InheritanceType.SING ...

  2. Linux部署之批量自动安装系统之DHCP篇

    1.         安装:yum install dhcp   2.         Ip配置信息   3.         Dhcp配置文件如下   4.         配置完后检查语法是否错误 ...

  3. RocketMQ学习笔记(5)----RocketMQ监控平台rocketmq-console-ng的搭建

    1. 下载rocketmq-console-ng 官网地址:https://github.com/apache/rocketmq-externals 拉下来之后,使用idea打开rocketmq-co ...

  4. 字符串格式化输出、while循环、运算符、编码

    1.字符串格式化输出 %占位符: %s => 字符串 %d=>整数型 %%=>转义 普通的% %()不能多,不能少,一一对应 f"{}"大括号里的内容一般都放变量 ...

  5. JWT的初步了解以及session、cookie机制

    1.什么是状态保持? 想要了解JWT,首先需要知道什么是状态保持,举一个例子来说:无论是在web上还是在手机app上,我们都可以以游客的身份访问,此时都会有登录/注册字眼,当我们登录之后,就会是我们的 ...

  6. C#-反射知识点

    1.反射是基于Sytem.Type的,里面的很多方法是system.reflection里面的 MethodInfo[] tt= t.GetType().GetMethods(); //t是strin ...

  7. code vs 1216 跳马问题

    1216 跳马问题  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 题目 输入描述 Input Descri ...

  8. Ubuntu设置显示桌面快捷键

    Ubuntu设置显示桌面快捷键 直接在系统设置中没有效果, 学习了:http://www.cnblogs.com/pluse/p/5286585.html 需要进行安装compizconfig,然后在 ...

  9. XCL-Chart柱形图的期望线/分界线

    周日在柱形图上加了两个小功能,当中之中的一个是加上了期望线/分界线,功能非常小,但我个人非常喜欢这个功能(好像之前也没看到别的图表库原生支持这个. ) 主要是加上这些小小的横线后,能非常明显的区分出数 ...

  10. codeforces 570 D Tree Requests

    题意:给出一棵树.每一个结点都有一个字母,有非常多次询问,每次询问.以结点v为根的子树中高度为h的后代是否可以经过调整变成一个回文串. 做法: 推断能否够构成一个回文串的话,仅仅须要知道是否有大于一个 ...