题目链接

Nlogonia is a queendom that consists of several cities located on a big mountain. The capital cityis Logville, located on the mountain peak. Logville has a huge lake with a perfectly round shape,appropriately named “The Big O”. This is the only lake with drinkable water in the entire queendom,so it is used to supply all cities. Some cities in Nlogonia are connected with water pipes that allow thedistribution of the water. As there are no pumps, each pipe carries water from a city to another cityat a lower altitude, using gravity.

Nlogonia’s water system has been a source of worries for the Queen, because since cities depend onother cities for their water supply, hot discussions occur about how much water a city is allowed to use.A water supply path is a sequence of cities in decreasing order of altitude, starting in Logville and suchthat there is a pipe connecting each pair of consecutive cities in the sequence. Two cities have disjointwater supply if and only if there exist two water supply paths, one supply path ending in each of thecities, such that Logville is the only city that is present in both paths. Notice that Logville itself hasdisjoint water supply with every other city.

The Queen considers disjoint water supply a nice property because it reduces dependency problemsand also avoids water outages to spread as quickly through Nlogonia. She therefore ordered a surveyto assess the current state of water supply disjointness in the whole queendom. Being the cleverestadvisors in the Queen’s court, you have been summoned to help calculate the number of pairs of distinctcities that have disjoint water supply.

Input

The input file contains several test cases, each of them as described below.
The first line contains two integers C (2 ≤ C ≤ 1000) and P (1 ≤ P ≤ 105), representing respectively

the number of cities and the number of water pipes in Nlogonia. Cities are identified with differentintegers from 1 to C, in strictly decreasing order of altitude (no two cities have the same altitude);Logville is city 1. Each of the next P lines describes a pipe with two integers U and V (1 ≤ U < V ≤ C)indicating that the pipe connects city U with city V . You may assume that no two pipes connect thesame pair of cities, and that for each city in Nlogonia there is at least one water supply path that endsin it.

Output

For each test case, output a line with an integer representing the number of pairs of distinct cities thathave disjoint water supply.

Sample Input

661213142526368 111213142534673637482656

Sample Output

1426

题意

1~n城市海拔从高到低,现在给出一些边,方向是从高到低。两个城市disjoint 当且仅当有两条路径从1出发,分别到这两个城市,且只在1节点相交。输出disjoint的城市有几对。

题解

方法1:分三类点,1. 与1直接相连,2. 所有1来的路径都必定经过某一点,3. 从1过来的路径没有必定经过的点。

第1和第3类点x,令f[x]=x,第2类点,令f[x]=p,p是必定经过的点的f。

第1类的好判断,判断2,3类需要利用已经计算出来的f:

因为边只能是海拔高到低的,下标小的海拔高,因此所有与 i 相连的比 i 小的点j 和k(j≠kj≠k)中若存在f[j]!=f[k],就是第3类,否则是第2类,p就是f[j]。

这样 f 相同的就是同一组,同一组内的就是不disjoint的。用总对数减去每组内的对数就是答案了。

方法2:

这题也可以用bitset水过去,用g[u]代表u必定经过的点,那么g[u]就是所有与其相连的所有前驱i的g[i]的交集。

方法1:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const int maxn=1000+100;
int head[maxn];
struct edge
{int to,next;
}e[maxn*200];   //
int tol=0;
void add(int u,int v)
{e[++tol].to=v,e[tol].next=head[u],head[u]=tol;
}
int f[maxn],num[maxn];
int main()
{int n,m;while(scanf("%d%d",&n,&m)!=EOF){memset(f,0,sizeof(f));memset(num,0,sizeof(num));memset(head,0,sizeof(head));tol=0;while(m--){int u,v;scanf("%d%d",&u,&v);add(v,u);}f[1]=1;rep(u,2,n+1){int ff=-1;bool flag=true;for(int i=head[u];i;i=e[i].next){int v=e[i].to;if(v==1){flag=false;break;}if(ff==-1) ff=f[v];else{if(ff!=f[v])flag=false;}}if(flag) f[u]=ff;else f[u]=u;}rep(i,1,n+1) num[f[i]]++;int ans=n*(n-1)/2;rep(i,2,n+1){ans-=num[i]*(num[i]-1)/2;}printf("%d\n",ans);}return 0;
}

方法2:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const int inf=0x3fffffff;
const ll mod=1000000007;
const int maxn=1e3+100;
const int maxm=1e5+100;
bitset<maxn> g[maxn];
int head[maxn];
struct edge
{int from,to,next;
}e[maxm];   //
int tol=0;
void add(int u,int v)
{e[++tol].to=v,e[tol].next=head[u],head[u]=tol;
}int main()
{int n,m;while(~scanf("%d%d",&n,&m)){tol=0;memset(head,0,sizeof(head));while(m--){int u,v;scanf("%d%d",&u,&v);add(u,v);}rep(i,1,n+1) g[i].set();g[1].reset();rep(u,1,n+1)  //g[i]表示结点i一定经过的点{if(u>1) g[u].set(u);for(int i=head[u];i;i=e[i].next){int v=e[i].to;g[v]&=g[u];}}int ans=0;rep(i,1,n+1)rep(j,i+1,n+1){g[0]=g[i]&g[j];if(g[0].count()==0) ans++;}printf("%d\n",ans);}return 0;
}

uvalive 6528(DAG,递推,想法/bitset, 好题)相关推荐

  1. 2013-11-09 实验之买不到的糖果数目(递推思维+思维逻辑题)

    题目:标题:买不到的数目     小明开了一家糖果店.他别出心裁:把水果糖包成4颗一包和7颗一包的两种.糖果不能拆包卖.     小朋友来买糖的时候,他就用这两种包装来组合.当然有些糖果数目是无法组合 ...

  2. bzoj#4161-Shlw loves matrixI【常系数线性齐次递推】

    正题 题目链接:https://darkbzoj.tk/problem/4161 题目大意 给出序列aaa,和hhh的0∼k−10\sim k-10∼k−1项,满足 hn=∑i=1naihn−ih_n ...

  3. YBT进阶一年游P1 递归与递推

    YBT进阶一年游P1 递归与递推 这一系列的题都来自于ybt高效进阶,相当有难度,所以以后尽可能坚持每一部分的题都写一篇博客进行总结.本人水平很一般,能力很有限,故解法和写法不一定(应该说是一定不)最 ...

  4. 关于南蛮图腾递推的问题

    待删 关于南蛮图腾递推的问题 原题来源于洛谷:P1498 南蛮图腾 题目链接 每个数据一个数字,表示图腾的大小(此大小非彼大小) n<=10 分析:设置循环以创建图形,每次循环则将宽高乘2,方便 ...

  5. 【递推】 铺砖2!!!

    铺砖2 序言 这篇博客我还是准备讲一讲递推这个板块的题.反正我是觉得基本上所有的递推题只要找到了递推式,那都是小case. 题目 现在上题: 题目描述 对于一个2行N列的走道.现在用1 * 2,2 * ...

  6. N阶楼梯上楼问题(递推问题)--C++实现

    题目描述 N阶楼梯上楼问题:一次可以走两阶或一阶,问有多少种上楼方式.(要求采用非递归) 输入描述: 输入包括一个整数N,(1<=N<90). 输出描述: 可能有多组测试数据,对于每组数据 ...

  7. P6134 [JSOI2015]最小表示(拓扑排序递推 + bitset优化,可达性统计变种)

    整理的算法模板合集: ACM模板 P6134 [JSOI2015] 题目要求删除一条边整个图的连通性是不受影响的,也就是说如果我们要删除边(x,y)(x,y)(x,y),删除以后整个图的连通性不受影响 ...

  8. 2019 ICPC Asia Nanjing Regional C.Digital Path(拓扑排序递推DP)

    整理的算法模板合集: ACM模板 题目传送门 三段题面,只有第三段是有用的-前两段又长单词又难懂,就是在讲故事...不过针对四种情况给出四个图帮助我们理解题意是真的赞,可能出题人怕我们看不懂吧(第一句 ...

  9. 解题报告:luogu P2272 [ZJOI2007]最大半连通子图(tarjan缩点、递推DP、hash、set判重)

    这时yxc上课时讲解的截图. 一般用到tarjan算法的题目步骤都非常相似: tarjan算法 缩点,建图(这里要判重) 按照拓扑序递推(这里缩点以后逆向就已经是拓扑序了)/ 循环遍历新图求解答案. ...

最新文章

  1. 深度学习:知识回收(Lecture1and2)
  2. 解决 Git: There is no tracking information for the current branch.的问题
  3. 小程序短视频项目———开发用户登录注册(一)
  4. Linux的make 命令出现:make:*** No targets specified and no makefile found.Stop
  5. python个人收支管理系统相关题目_练手题:计算人均付费(SQLPython)
  6. 存储盟主将要退位云计算会是最后盟主?
  7. 论文阅读-可变形卷积v2: More Deformable, Better Results
  8. 遗传算法适应度计算函数——ranking
  9. excel二极管伏安特性曲线_电视机不被烧,是因为它?一个拥有反向特性的稳压二极管...
  10. Excel如何判断数据是否重复
  11. matlab函数power,Matlab中Powergui介绍.pdf
  12. 计算机应用能力考试ppt2003,全国专业技术人员计算机应用能力考试_PPT_2003_题库版...
  13. Latex——数学符号大全
  14. 东华大学专业英语 词汇学习
  15. 这2个在线资源网千万别错过,老司机人手一份,一年省下好几千
  16. 【项目管理】软件项目经理述职报告
  17. BCI Competition 2008 – Graz dataset A个人翻译(附MATLAB安装BioSig)
  18. 维护高 Star Github 项目,会遇到什么有趣的问题 2022 版
  19. 重庆已建成4.2万余个5G基站;山西省累计建成5G基站1.2万个
  20. java 内存检测工具

热门文章

  1. 腾讯企业邮箱开通过程中需要添加那些域名解析?
  2. ajax如何使用,页面如何调试
  3. 手把手教你智能硬件开发(六)直流马达
  4. C++操作http之WinInet详解
  5. Vivado 2017.4版本下载
  6. QT中QVector的使用
  7. [初级前端工程师]网络相关知识
  8. jetracer——自动驾驶车项目(basic_motion.ipynb)
  9. numpy与Image互转
  10. 横滚角,俯仰角,航向角