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 ...
随机推荐
- php——数据库操作之规范性
今天在写一个项目,上传到服务器的时候出现500的错误,找了半天最后是因为数据库更新数据的语句写的不规范, 询问同事之后,同事说,数据库的增删改查语句写的不规范的时候有的时候会报错有的时候不会: 所以总 ...
- css3 改变默认选中文本背景色和文本颜色
::selection { background:#d3d3d3; color:#555; } ::-moz-selection { background:#d3d3d3; color:#555; } ...
- Struts2的值栈和OGNL牛逼啊
Struts2的值栈和OGNL牛逼啊 一 值栈简介: 值栈是对应每个请求对象的一套内存数据的封装,Struts2会给每个请求创建一个新的值栈,值栈能够线程安全的为每个请求提供公共的数据存取服务. 二 ...
- codeforces 946G
题意: 有一个长度为n的数组a.你可以删除一个位置之后进行操作,一次操作可以把任意位置上的数字变成任意的值,问最少需要多少操作能使得数列变成严格上升的. n<=200000 分析: 如果没有删除 ...
- JDBC自定义连接池
开发中,"获得连接"和"释放资源"是非常消耗系统资源的,为了解决此类性能问题可以采用连接池技术来共享连接Connection. 1.概述 用池来管理Connec ...
- Python的未来和Python的意义 & pypy & JIT
今天在读关于Lisp的文章,感概于这门语言的生命力(Link).同时也读到了关于python的文章,Python之父谈Python的未来(Link) 文章中拿Python和Javascript作比较, ...
- 数据库系统学习(九)-嵌入式SQL语言之基本技巧
第九讲 嵌入式SQL语言之基本技巧 901 什么是嵌入式SQL语言 交互式SQL语言的局限性 嵌入式SQL语言 交互式和嵌入式语言的对比 高级语言中使用嵌入式语言需要解决的问题 902 程序与数据库连 ...
- 按字节寻址and按字编址
按字节寻址,存储空间的最小编址单位是字节,按字编址,存储空间的最小编址单位是字,最小编址单位的选择,和存储容量.地址总线宽度都有关联 .同样的存储容量, 粒度小了,地址长度就需要更长 .就是根据不同的 ...
- spinlock,mutex,semaphore,critical section的作用与差别
某年深信服的笔试题,考的就是多线程的同步.简单的解释下方便记忆: 1.spinlock:自旋锁.是专为防止多处理器并发而引入的一种锁. 2.mutex:相互排斥量. 仅仅有拥有相互排斥对象的线程才有訪 ...
- 【日常学习】【二叉树遍历】Uva548 - Tree题解
这道题目本身不难,给出后序遍历和中序遍历,求到节点最小路径的叶子,同样长度就输出权值小的叶子. Uva上不去了,没法測.基本上是依照ruka的代码来的.直接上代码 //Uva548 Tree #inc ...