HDU_6016_(Bestcoder round #92 1002)_(dfs)(暴力)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6016
题意:给定男羊和女羊的朋友关系,即给定一个图,问从任意一只羊开始连续数四只不相同的羊的方法数。
思路:一开始用了dfs没过,报的超时,以为真的会超时,最后发现,犯了个以前常犯的老问题,存边的vector开小了,,这道题需要开
(n+m)的vector。
后来又按官方题解做了一遍,官方题解,也就需要脑袋转一下。要找序列:男--女--男--女,或者 女--男--女--男;那么只需要找其中一种,即只找 女(1)--男(2)--女(3)--男(4) 的种数,答案为ans*2。枚举(2)号男羊和与(2)号男羊之间有边的一只(3)号女羊,即可确定
一类序列的种数,ans+=(sheep[i]-1)*(sheep[j]-1) i与j之间有边。
dfs:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
#define N 100005 vector<int> sheep[*N]; int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=n+m;i++)
sheep[i].clear();
for(int i=;i<k;i++)
{
int a,b;
scanf("%d%d",&a,&b);
sheep[a].push_back(b+n);
sheep[b+n].push_back(a);
}
long long ans=;
for(int i=;i<=n;i++)
{
int way=sheep[i].size()-;
for(int j=;j<sheep[i].size();j++)
{
ans+=(sheep[sheep[i][j]].size()-)*way;
}
}
printf("%I64d\n",ans*);
}
return ;
}
官方做法:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
#define N 100005 vector<int> sheep[*N]; int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=n+m;i++)
sheep[i].clear();
for(int i=;i<k;i++)
{
int a,b;
scanf("%d%d",&a,&b);
sheep[a].push_back(b+n);
sheep[b+n].push_back(a);
}
long long ans=;
for(int i=;i<=n;i++)
{
int way=sheep[i].size()-;
for(int j=;j<sheep[i].size();j++)
{
ans+=(sheep[sheep[i][j]].size()-)*way;
}
}
printf("%I64d\n",ans*);
}
return ;
}
HDU_6016_(Bestcoder round #92 1002)_(dfs)(暴力)的更多相关文章
- BestCoder Round #92 1002 Count the Sheep —— 枚举+技巧
题目链接:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=748&pid=1002 题解: 做题的时候只是想到 ...
- 暴力+降复杂度 BestCoder Round #39 1002 Mutiple
题目传送门 /* 设一个b[]来保存每一个a[]的质因数的id,从后往前每一次更新质因数的id, 若没有,默认加0,nlogn复杂度: 我用暴力竟然水过去了:) */ #include <cst ...
- Manacher BestCoder Round #49 ($) 1002 Three Palindromes
题目传送门 /* Manacher:该算法能求最长回文串,思路时依据回文半径p数组找到第一个和第三个会文串,然后暴力枚举判断是否存在中间的回文串 另外,在原字符串没啥用时可以直接覆盖,省去一个数组空间 ...
- 二分图判定+点染色/并查集 BestCoder Round #48 ($) 1002 wyh2000 and pupil
题目传送门 /* 二分图判定+点染色:因为有很多联通块,要对所有点二分图匹配,若不能,存在点是无法分配的,no 每一次二分图匹配时,将点多的集合加大最后第一个集合去 注意:n <= 1,no,两 ...
- 矩阵快速幂---BestCoder Round#8 1002
当要求递推数列的第n项且n很大时,怎么快速求得第n项呢?可以用矩阵快速幂来加速计算.我们可以用矩阵来表示数列递推公式比如fibonacci数列 可以表示为 [f(n) f(n-1)] = [f(n ...
- 贪心/二分查找 BestCoder Round #43 1002 pog loves szh II
题目传送门 /* 贪心/二分查找:首先对ai%=p,然后sort,这样的话就有序能使用二分查找.贪心的思想是每次找到一个aj使得和为p-1(如果有的话) 当然有可能两个数和超过p,那么an的值最优,每 ...
- hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]
传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131 ...
- ACM学习历程—HDU5269 ZYB loves Xor I(位运算 && dfs && 排序)(BestCoder Round #44 1002题)
Problem Description Memphis loves xor very musch.Now he gets an array A.The length of A is n.Now he ...
- BestCoder Round #56 1002 Clarke and problem 1003 Clarke and puzzle (dp,二维bit或线段树)
今天第二次做BC,不习惯hdu的oj,CE过2次... 1002 Clarke and problem 和Codeforces Round #319 (Div. 2) B Modulo Sum思路差不 ...
随机推荐
- 【bzoj2654]】tree
给白色边都加上一个值,二分这个值,使得选取的白边数量减少 #include<algorithm> #include<iostream> #include<cstdlib& ...
- ugc pgc ogc web2.0 mgc
http://yjy.people.com.cn/n/2014/0120/c245079-24169402.html machine
- 在64位的ubuntu 14.04 上开展32位Qt 程序开发环境配置(pro文件中增加 QMAKE_CXXFLAGS += -m32 命令)
为了能中一个系统上开发64或32位C++程序,费了些周折,现在终于能够开始干过了.在此记录此时针对Q5.4版本的32位开发环境配置过程. 1. 下载Qt 5.4 的32位版本,进行安装,安装过程中会发 ...
- go4--break,continue + 标签
package main /* 指针 Go虽然保留了指针,但与其它编程语言不同的是,在Go当中不 支持指针运算以及”->”运算符,而直接采用”.”选择符来操作指针 目标对象的成员 操作符”&am ...
- git 查看、切换用户
查看命令: 查看用户名: git config user.name 查看用户邮箱: git config user.email 修改命令: 修改用户名: git config --global use ...
- BZOJ2038 小Z的袜子 (莫队算法)
题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=2038 专题练习: http://acm.hust.edu.cn/vjudge/conte ...
- bzoj4407
http://www.lydsy.com/JudgeOnline/problem.php?id=4407 以前写过一次线性筛 发现不是很理解 写了个欧拉筛的 t了 其实每次推式子,都会先推出一组的解法 ...
- HTML5中File
一 File对象与FileList对象 当将input元素的type类型设置为file时,web页面上会显示一个选择文本按钮和一个文本显示框,单击文件按钮可以选择一个文件,文本显示框中会显示选中的文件 ...
- Java DAO模式
DAO模式: DAO(DateAccessObject,数据存取对象) 位于业务逻辑和持久化数据之间,实现对持久化数据的访问. DAO组成: DAO接口: 数据库的所有操作定义成抽象方法,可以提供多种 ...
- P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm(Tarjan+记忆化)
P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题意翻译 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N< ...