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优化)的更多相关文章

  1. 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 ...

  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 ...

  3. hdu 5745 La Vie en rose DP + bitset优化

    http://acm.hdu.edu.cn/showproblem.php?pid=5745 这题好劲爆啊.dp容易想,但是要bitset优化,就想不到了. 先放一个tle的dp.复杂度O(n * m ...

  4. hdu_5036_Explosion(bitset优化传递闭包)

    题目链接:hdu_5036_Explosion 题意: 一个人要打开或者用炸弹砸开所有的门,每个门里面有一些钥匙,一个钥匙对应一个门,有了一个门的钥匙就能打开相应的门,告诉每个门里面有哪些门的钥匙,问 ...

  5. HDU4460-Friend Chains-BFS+bitset优化

    bfs的时候用bitset优化一下. 水题 #include <cstdio> #include <cstring> #include <algorithm> #i ...

  6. HDU5745-La Vie en rose-字符串dp+bitset优化

    这题现场的数据出水了,暴力就能搞过. 标解是拿bitset做,转移的时候用bitset优化过的操作(与或非移位)来搞,复杂度O(N*M/w) w是字长 第一份标程的思路很清晰,然而后来会T. /*-- ...

  7. bzoj2208 连通数(bitset优化传递闭包)

    题目链接 思路 floyd求一下传递闭包,然后统计每个点可以到达的点数. 会tle,用bitset优化一下.将floyd的最后一层枚举变成bitset. 代码 /* * @Author: wxyww ...

  8. POJ 3275 Ranking the Cows(传递闭包)【bitset优化Floyd】+【领接表优化Floyd】

    <题目链接> 题目大意:FJ想按照奶牛产奶的能力给她们排序.现在已知有N头奶牛$(1 ≤ N ≤ 1,000)$.FJ通过比较,已经知道了M$1 ≤ M ≤ 10,000$对相对关系.每一 ...

  9. Gym 100342J Triatrip (求三元环的数量) (bitset优化)

    <题目链接> 题目大意:用用邻接矩阵表示一个有向图,现在让你求其中三元环的数量. 解题分析:先预处理得到所有能够直接到达每个点的集合$arrive[N]$和所有能够由当前点到达的集合$to ...

随机推荐

  1. DispatcherServlet url-pattern中 /、/*、*.do中的区别与作用

    DispatcherServlet url-pattern中 /./*.*.do中的区别与作用 "/'表示匹配所有请求(其中包含除.jsp和.jspx外的所有后缀). 如果不配置静态资源,它 ...

  2. T1405 奶牛的旅行 codevs

    http://codevs.cn/problem/1405/ 时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 农民John的农场 ...

  3. 洛谷——P3576 [POI2014]MRO-Ant colony

    P3576 [POI2014]MRO-Ant colony 题目描述 The ants are scavenging an abandoned ant hill in search of food. ...

  4. Linux索引节点(Inode:no space for device)用满导致的一次故障

    问题描写叙述 在storm測试环境集群上上nimbus和supervisor自己主动挂调.重新启动时显示no space for device,也不能创建,加入文件及文件夹,df -h查看 ilesy ...

  5. 转:libev和libevent的设计差异

    转:http://www.cnblogs.com/Lifehacker/p/whats_the_difference_between_libevent_and_libev_chinese.html [ ...

  6. react jsx 数组变量的写法

    1.通过 map 方法 var students = ["张三然","李慧思","赵思然","孙力气","王萌 ...

  7. 【转载】NULL,"",String.Empty三者在C#中的区别

    (1)NULLnull 关键字是表示不引用任何对象的空引用的文字值.null 是引用类型变量的默认值.那么也只有引用型的变量可以为NULL,如果int i=null,的话,是不可以的,因为Int是值类 ...

  8. ucgui界面设计演示样例2

    ucgui界面设计演示样例2 本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 环境: 主机:WIN8 开发环境:MDK4.72 ucgui版本号:3 ...

  9. 获取DOM父元素和子元素

    利用javascript可以遍历DOM树,这篇文章介绍用获取一个DOM元素的所有父节点和获取一个DOM元素的所以子孙节点. 1.获取所有父节点.用递归的方法,用parentNode属性. <!D ...

  10. docker compose环境搭建

    概述 Docker Compose提供一个简单的基于YAML配置语言.用于描写叙述和组装多容器的分布式应用. 使用docker定义和执行复杂的应用.使用compose,能够在一个文件中,定义多容器的应 ...