SPOJ:Dandiya Night and Violence(Bitset优化)
It is Dandiya Night! A certain way how dandiya is played is described:
There are N pairs of people playing at a time. Both the person in a pair are playing Dandiya with each other. Since a person might get bored with the same partner, he can swap with a friend in a different pair. For example, if (1, 2) and (3, 4) are initial pairs, and if 1 and 3 are friends, they can swap, and a possible configuration of pairs will be (3, 2) and (1, 4). Friendship relation is transitive in nature. (x,y) and (y, z) friendship pairs imply a (x, z) friendship pair.
Now, Dandiyas are dangerous if not used carefully, and there are always pairs of people who would like to engage in a violent dandiya encounter. A violent dandiya encounter occurs in a pair (5, 6) if 5 and 6 are enemies (not friends). ACM is present at the Dandiya Night and is concerned about this situation.
Given the initial arrangement of pairs, help us to determine the maximum number of violent dandiya encounters possible over the entire Dandiya Night.
Note: A pair (x, y) is unordered, i.e., both (x, y) and (y, x) should be considered the same.
Input
First line denotes number of test cases T.
T test cases follow.
Each test case is formatted as First line consist of integers N, F (N = Number of pairs, F = Number of Friend pairs)
N lines follow, each consisting of two integers, which denote an initial pair of Dandiya Night
(People are numbered from 1 to 2*N)
F lines follow, each denoting a pair of friends.
T<=100
1<=N<=200
0<=F<=min(5000, C(2*N, 2)) (C(n, k) = Binomial Coefficient)
Output
For each Test case, output a line consisting of an integer denoting the maximum possible violent dandiya encounters.
Example
Input:
22 11 23 41 34 31 23 45 67 81 22 35 4
Output:
4
9
题意:有2*N个人,开始他们组好了队比赛,而且知道他们之间的好友关系(F组),好友的好友也是自己的好友;比赛时,好友可以换位置,问可能产生多少组队,两个成员不是好友。有T组数据。
思路:模拟即可,但是注意必须将N^3*T优化为N^2*T或者更优。需要bitset。同时,注意不要用mp取更新q。
(建议自己写一发,才知道这题蛮坑的!
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
bitset<maxn>mp[maxn];
int vis[maxn][maxn];
int q[maxn*maxn][],head,tail;
int main()
{
int T,N,M,x,y,k,i,j,ans;
scanf("%d",&T);
while(T--){
scanf("%d%d",&N,&M); head=tail=ans=;
for(i=;i<=N+N;i++)
for(j=;j<=N+N;j++)
vis[i][j]=;
for(i=;i<=N+N;i++) mp[i].reset();
for(i=;i<=N;i++){
scanf("%d%d",&x,&y);
if(x>y) swap(x,y);
if(!vis[x][y]){
q[++head][]=x; q[head][]=y;
vis[x][y]=;
}
}
N<<=;
for(i=;i<=M;i++){
scanf("%d%d",&x,&y);
mp[x][y]=mp[y][x]=;
}
for(k=;k<=N;k++)
for(i=;i<=N;i++)
if(mp[i][k])
mp[i]|=mp[k];
while(tail<head){
tail++;
x=q[tail][]; y=q[tail][];
for(i=;i<=N;i++){
int ty=y; if(ty>)
if(mp[x][i]&&!vis[i][y]) q[++head][]=i,q[+head][]=y,vis[i][y]=;
}
for(i=;i<=N;i++) if(mp[y][i]&&!vis[x][i]) q[++head][]=x,q[+head][]=i,vis[x][i]=;
}
printf("%d\n",ans);
}
return ;
}
SPOJ:Dandiya Night and Violence(Bitset优化)的更多相关文章
- SPOJ:Harbinger vs Sciencepal(分配问题&不错的DP&bitset优化)
Rainbow 6 is a very popular game in colleges. There are 2 teams, each having some members and the 2 ...
- hdu 5506 GT and set dfs+bitset优化
GT and set Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Probl ...
- hdu 5745 La Vie en rose DP + bitset优化
http://acm.hdu.edu.cn/showproblem.php?pid=5745 这题好劲爆啊.dp容易想,但是要bitset优化,就想不到了. 先放一个tle的dp.复杂度O(n * m ...
- hdu_5036_Explosion(bitset优化传递闭包)
题目链接:hdu_5036_Explosion 题意: 一个人要打开或者用炸弹砸开所有的门,每个门里面有一些钥匙,一个钥匙对应一个门,有了一个门的钥匙就能打开相应的门,告诉每个门里面有哪些门的钥匙,问 ...
- HDU4460-Friend Chains-BFS+bitset优化
bfs的时候用bitset优化一下. 水题 #include <cstdio> #include <cstring> #include <algorithm> #i ...
- HDU5745-La Vie en rose-字符串dp+bitset优化
这题现场的数据出水了,暴力就能搞过. 标解是拿bitset做,转移的时候用bitset优化过的操作(与或非移位)来搞,复杂度O(N*M/w) w是字长 第一份标程的思路很清晰,然而后来会T. /*-- ...
- bzoj2208 连通数(bitset优化传递闭包)
题目链接 思路 floyd求一下传递闭包,然后统计每个点可以到达的点数. 会tle,用bitset优化一下.将floyd的最后一层枚举变成bitset. 代码 /* * @Author: wxyww ...
- POJ 3275 Ranking the Cows(传递闭包)【bitset优化Floyd】+【领接表优化Floyd】
<题目链接> 题目大意:FJ想按照奶牛产奶的能力给她们排序.现在已知有N头奶牛$(1 ≤ N ≤ 1,000)$.FJ通过比较,已经知道了M$1 ≤ M ≤ 10,000$对相对关系.每一 ...
- Gym 100342J Triatrip (求三元环的数量) (bitset优化)
<题目链接> 题目大意:用用邻接矩阵表示一个有向图,现在让你求其中三元环的数量. 解题分析:先预处理得到所有能够直接到达每个点的集合$arrive[N]$和所有能够由当前点到达的集合$to ...
随机推荐
- 大话Spark(3)-一图深入理解WordCount程序在Spark中的执行过程
本文以WordCount为例, 画图说明spark程序的执行过程 WordCount就是统计一段数据中每个单词出现的次数, 例如hello spark hello you 这段文本中hello出现2次 ...
- 在C#中使用Json.Net进行序列化和反序列化及定制化
序列化(Serialize)是将对象转换成字节流,并将其用于存储或传输的过程,主要用途是保存对象的状态,以便在需要时重新创建该对象:反序列化(Deserialize)则是将上面的字节流转换为相应对象的 ...
- mysql索引底层的数据结构和算法
1. 为什么要用索引 索引在MySQL中也叫做“键”,是存储引擎用于快速找到记录的一种数据结构.索引对于良好的性能非常关键,尤其是当表中的数据量越来越大时,索引对于性能的影响愈发重要. 索 ...
- 《Java虚拟机原理图解》1.3、class文件中的访问标志、类索引、父类索引、接口索引集合
讲完了class文件中的常量池,我们就相当于克服了class文件中最麻烦的模块了.现在,我们来看一下class文件中紧接着常量池后面的几个东西:访问标志.类索引.父类索引.接口索引集合. 1. 访问标 ...
- C#使用PrintDocument打印 多页 打印预览
PrintDocument实例所有的订阅事件如下: 创建一个PrintDocument的实例.如下: System.Drawing.Printing.PrintDocument docToPrint ...
- iOS开发 检测版本更新
iOS开发 检测版本更新的实现 苹果给了我们一个接口,能根据应用id请求一些关于应用的信息.我们可以根据返回的信息,来判断版本是否和应用的版本一致,如果不一致,那么就出现新的版本了.这时,就需要向用户 ...
- tensorflow基础练习:线性模型
TensorFlow是一个面向数值计算的通用平台,可以方便地训练线性模型.下面采用TensorFlow完成Andrew Ng主讲的Deep Learning课程练习题,提供了整套源码. 线性回归 多元 ...
- jQuery -> 获取后代元素的三种方法
假设我们有内容例如以下的html文件,那么怎样选取包括在<p>元素内的<i>元素呢? 邪馬台国の謎と弥生時代 紀元前1000年ごろ.水稲工作の技術をもつ集団が大挙して日本に移住 ...
- VC++_错误 无法打开包括文件“glglut.h” No such file or directory 怎么办
在网上看到类似的问题,查找资料找到了解决方案,现整理如下,有些更改,好让自己多些印象,附原文网址:http://blog.csdn.net/bigloomy/article/details/62265 ...
- 函数柯里化 curry
一.函数柯里化的特性: (1)参数复用 $.ajax // 示例一 function ajax(type,url,data) { var xhr = new XMLHttpRequest(); xhr ...