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. sftp权限

    用户: t1 t2 根目录: /home/data/ /home/data/t1/ drwxr-xr-x. 2 t1 t1 4096 Mar 24 17:26 t1 /home/data/t2/ dr ...

  2. Python 计算相似度

    #计算相似度 #欧式距离 # npvec1, npvec2 = np.array(det_a), np.array(det_b) # similirity=math.sqrt(((npvec1 - n ...

  3. Java XSSF 导出excel 工具类

    参数解释: title:导出excel标题.headers 导出到excel显示的列头. columns 对应数据库字段 .list 导出数据1.pox中添加依赖 <dependency> ...

  4. javaweb实现教师和教室管理系统 java jsp sqlserver

    1,程序设计思想 (1)设计三个类,分别是工具类(用来写连接数据库的方法和异常类的方法).信息类(用来写存储信息的方法).实现类(用来写各种操作数据库的方法) (2)定义两个jsp文件,一个用来写入数 ...

  5. 如何配置任意目录下Web应用程序

    1,首先创建一个Web项目,tomcat 7, JDK 1.8 2,创建Web项目并部署到tomcat服务器下运行的步骤和方法: 在Eclipse下创建一个JAVA project 在JAVA项目下创 ...

  6. [MST] Create an Entry Form to Add Models to the State Tree

    It is time to add new entries to the wishlist. We will achieve this by reusing forms and models we'v ...

  7. hdu 1978 记忆化搜索

    注意: dp[i][j] 表示(i,j)这个点有多少种方式       mark[i][j]表示这个点是否走过  假设有直接返回dp[i][j]    dp的求法为全部梦走到点的dp的和 注意mark ...

  8. Swift学习——使用if和switch来进行条件操作,使用for,while,和do-while来进行循环(三)

    Swift学习--使用if和switch来进行条件操作,使用for,while,和do-while来进行循环 //switch支持随意类型的数据以及各种比較操作--不不过整数以及測试相等 //注意假设 ...

  9. 【android】解决Viewpager设置高度为wrap_content无效的方法

    今天发现设置viewpager高度为wrap_content时并没作用.stackoverflow给出了解决方式,就是自己定义viewpager,重写onMesure()方法: public clas ...

  10. mysql---左连接、右连接、内连接之间的差别与联系

    现有两张表 第一张表为男生表,记录了男生的姓名和配偶的编号 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbXlfbWFv/font/5a6L5L2T/fo ...