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. django cookie session操作

    Cookie是什么? cookie说的直白点就是保存在用户浏览器端的一个键值对,举个例子,你现在登录了京东商城,你把浏览器关闭之后,你再打开京东,你还是可以对你的账户继续操作,已经购买的商品,订单都是 ...

  2. 《Java虚拟机原理图解》 1.2、class文件中的常量池

    了解JVM虚拟机原理 是每一个Java程序员修炼的必经之路.但是由于JVM虚拟机中有很多的东西讲述的比较宽泛,在当前接触到的关于JVM虚拟机原理的教程或者博客中,绝大部分都是充斥的文字性的描述,很难给 ...

  3. JS --- 数组循环要用length

    socket.on("receive", function (data) { deviceone.print("返回的数据:"+data) // 发送异常 va ...

  4. svm、logistic regression对比

    相同点:都是线性分类算法 不同点: 1.损失函数不同 LR:基于“给定x和参数,y服从二项分布”的假设,由极大似然估计推导 SVM: hinge loss + L2 regularization的标准 ...

  5. table合并单元格

    table合并单元格 新建一张表格.要求表格有四行四列,当中第一行的四列合并,第二行.第三行和第四行的第一列合并 <!DOCTYPE html PUBLIC "-//W3C//DTD ...

  6. stl_内存基本处理工具

    内存基本处理工具 STL定义5个全局函数.作用于初始化空间上.各自是:用于构造的construct(),用于析构的destroy(),uninitialized_copy(),uninitialize ...

  7. free命令具体解释——Linux性能分析

    一.使用格式 语法格式:free [-b | -k | -m] [-o] [-s delay ] [-t] [-l] [-V] [-b | -k | -m] :选择数据的单位-b字节.-k千字节.-m ...

  8. C++ string string string string string string string string string string

    一. 初始化 string s1="i love you"; string s2(s1); //把s2初始化为string s1,注意不能写成string s2; s2(s1); ...

  9. hive:Access denied for user &#39;root&#39;@&#39;%&#39;

    配置hive全分布模式时候,在mysql里面创建用户:create user 'hive' identified by 'hive'; 然后给hive帐号分配全部权限: grant all privi ...

  10. Material Design (四),AppBarLayout的使用

    前言  AppBarLayout,顾名知意.就是用来给AppBar布局的容器,是LinearLayout的子类.而AppBar就包括我们通常所知道的ActionBar,Toolbar. AppBarL ...