http://acm.hdu.edu.cn/showproblem.php?pid=3357

给出公司间的控股关系,问有多少组不合法数据,自己控股自己不合法,a控股b,b控股c,则a控股c

其实就是找环,加一条边如果出现环ans++,但是每次搜一遍有没有环会tle。此处用邻接矩阵处理,如果a要控股b,则控股a的公司都控股b,并且a控股的公司都被控股a的控股。对于b的分析同理,此题有大量重复数据,需要去掉

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#include <queue>
#include <cmath>
#include <stack>
#include <set> using namespace std; int mp[][];
int n; void add(int a,int b){
mp[a][b]=;
for(int i=;i<=n;i++)
if(mp[i][a]){
mp[i][b]=;
for(int j=;j<=n;j++)
if(mp[a][j])
mp[i][j]=;
}
for(int i=;i<=n;i++)
if(mp[b][i]){
mp[a][i]=;
for(int j=;j<=n;j++)
if(mp[j][b])
mp[j][i]=;
}
} int main(){
int t;
int cas=;
while(~scanf("%d%d",&n,&t)){
if(!n && !t)break;
memset(mp,,sizeof(mp));
int ans=;
while(t--){
int a,b;
scanf("%d%d",&a,&b);
if(mp[b][a])ans++;
else if(a==b)ans++;
else if(!mp[a][b]) add(a,b);//关键,去除重复边,直接else会TLE
}
printf("%d. %d\n",cas++,ans);
}
return ;
}

HDU 3357的更多相关文章

  1. hdu 3357 水题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3357 #include <cstdio> #include <cmath> # ...

  2. hdu 3357 Stock Chase (图论froyd变形)

    Stock Chase Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. 转载:hdu 题目分类 (侵删)

    转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...

  4. poj和hdu部分基础算法分类及难度排序

    最近想从头开始刷点基础些的题,正好有个网站有关于各大oj的题目分类(http://www.pythontip.com/acm/problemCategory),所以写了点脚本把hdu和poj的一些题目 ...

  5. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  7. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  8. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  9. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

随机推荐

  1. centos彻底删除文件夹、文件命令

    [1]新建文件夹 格式:mkdir 文件名 view source1 mkdir /home/test     新建一个名为test的文件夹在home下 [2]新建文本 vi /home/test.s ...

  2. Firefox火狐Flash插件卡死问题完美解决方法(转载)

    http://www.ihacksoft.com/firefox-flash-protectedmode.html 其实这个问题以前就出现过,而最近该问题又出现在最新的 Windows 8.1 系统中 ...

  3. bzoj2458: [BeiJing2011]最小三角形(分治+几何)

    题目链接:bzoj2458: [BeiJing2011]最小三角形 学习推荐博客:分治法编程问题之最接近点对问题的算法分析 题解:先将所有点按x值排列,然后每次将当前区间[l,r]分成左右两半递归求解 ...

  4. 454. 4Sum II ——查找本质:hash最快,二分次之

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  5. node 事件循环

    什么是事件循环 Node只运行在一个单一线程上,至少从Node.js开发者的角度是这样的.在底层, Node是通过libuv来实现多线程的. Libuv库负责Node API的执行.它将不同的任务分配 ...

  6. 在 Visual C# 项目中调用 VBA 中的代码

    https://msdn.microsoft.com/zh-cn/library/Bb608613.aspx http://www.cnblogs.com/yangbin1005/archive/20 ...

  7. spring+springmvc+mybatis整合

    1.web.xml配置 <?xml version="1.0" encoding="UTF-8"?> <web-app version=&qu ...

  8. java8Lambda详解

    [移步] http://blog.csdn.net/ioriogami/article/details/12782141/

  9. DQL查询语句内容整理

    select * from t_hq_ryxx; select bianh,xingm from t_hq_ryxx; --为字段名定义别名 select bianh as 编号,xingm as 姓 ...

  10. P121 6.7 第一题和第二题

    package nothh; import java.util.Arrays; public class shuzu6_7 { public static void main(String[] arg ...