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:
2
2 1
1 2
3 4
1 3
4 3
1 2
3 4
5 6
7 8
1 2
2 3
5 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 ...
随机推荐
- 接阿里云oss有感
看API,从头细看到尾,在这个过程中一定会找到你要找的东西.
- C#使用PrintDocument打印 多页 打印预览
PrintDocument实例所有的订阅事件如下: 创建一个PrintDocument的实例.如下: System.Drawing.Printing.PrintDocument docToPrint ...
- Android应用开发 WebView与服务器端的Js交互
最近公司再添加功能的时候,有一部分功能是用的html,在一个浏览器或webview中展示出html即可.当然在这里我们当然用webview控件喽 WebApp的好处: 在应用里嵌套web的好处有这么几 ...
- centos 7 卸載 mysql
跟網上文章,安裝了一個mysqlwget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm 記下卸載過程: 首先执行查看命令 ...
- SolidEdge如何快速绘制并完全定义槽型孔
如果你点击A之后形成的圆弧不是你想要的 你试着换个方向,如下图所示 有时候只有一个方形可以形成你要的半圆
- PAT 1003 Sharing (25)
题目描写叙述 To store English words, one method is to use linked lists and store a word letter by letter. ...
- 【试水CAS-4.0.3】第06节_CAS服务端配置HTTPS
完整版见https://jadyer.github.io/2012/05/30/tomcat-https/ /** * @see CAS服务端配置HTTPS * @see -------------- ...
- 【转载】.NET Remoting学习笔记(二)激活方式
目录 .NET Remoting学习笔记(一)概念 .NET Remoting学习笔记(二)激活方式 .NET Remoting学习笔记(三)信道 参考:百度百科 ♂风车车.Net 激活方式概念 在访 ...
- register_shutdown_function函数详解
设定错误和异常处理三函数 register_shutdown_function(array(‘Debug’,'fatalError’)); //定义PHP程序执行完成后执行的函数 set_error_ ...
- ORA-07445 第一參数为:kkqljpmpr
在版本号11.2.0.1.0上,在pl/sql developer中运行一条SQL会导致连接中断,这样的错误要到trace文件夹下找到错误日志文件,再定位.查了一下资料,是这个版本号的bug. D ...