【Codeforces 489D】Unbearable Controversy of Being
【链接】 我是链接,点我呀:)
【题意】
让你找到(a,b,c,d)的个数
这4个点之间有4条边有向边
(a,b)(b,c)
(a,d)(d,c)
即有两条从a到b的路径,且这两条路径分别经过b和d到达c
【题解】
我们枚举a,c
然后找到这样的b的个数cntb,其中a到b有一条边,b到c也有一条边
显然从这cntb个b中取2个
就能组成b和d了。
即C(N,2)
怎么得到这样的b的个数呢
先枚举a的所有出点v,然后看看v是否和c联通即可.
联通的话,cntb++
【代码】
import java.io.*;
import java.util.*;
public class Main {
static InputReader in;
static PrintWriter out;
public static void main(String[] args) throws IOException{
//InputStream ins = new FileInputStream("E:\\rush.txt");
InputStream ins = System.in;
in = new InputReader(ins);
out = new PrintWriter(System.out);
//code start from here
new Task().solve(in, out);
out.close();
}
static int N = 3000;
static class Task{
int n,m;
ArrayList<Integer> g[] = new ArrayList[N+10];
boolean can[][] = new boolean[N+10][N+10];
public void solve(InputReader in,PrintWriter out) {
for (int i = 1;i <= N;i++) {
g[i] = new ArrayList<Integer>();
}
n = in.nextInt(); m = in.nextInt();
for (int i = 1;i <= m;i++) {
int x,y;
x = in.nextInt();y = in.nextInt();
can[x][y] = true;
g[x].add(y);
}
long ans = 0;
for (int i = 1;i <= n;i++)
for (int j = 1;j <= n;j++)
if (i!=j) {
long numberofpath2 = 0;
for (int l = 0;l < (int)g[i].size();l++) {
int y = g[i].get(l);
if (can[y][j]) {
numberofpath2++;
}
}
ans = ans + numberofpath2*(numberofpath2-1)/2;
}
out.println(ans);
}
}
static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer;
public InputReader(InputStream ins) {
br = new BufferedReader(new InputStreamReader(ins));
tokenizer = null;
}
public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
【Codeforces 489D】Unbearable Controversy of Being的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【Codeforces 670C】 Cinema
[题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...
- 【codeforces 515D】Drazil and Tiles
[题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...
随机推荐
- 转贴:获取元素CSS值之getComputedStyle方法熟悉
获取元素CSS值之getComputedStyle方法熟悉 一.碎碎念~前言 我们都用过jQuery的CSS()方法,其底层运作就应用了getComputedStyle以及getPropertyVal ...
- Java 链式写法
Java链式写法,子类继承父类的属性,也可以返回子类的对象,只是需要重写基类的Set方法 public class MyLS { public static void main(String[] ar ...
- bzoj 1585: [Usaco2009 Mar]Earthquake Damage 2 地震伤害【最小割】
枚举建图.jpg 一开始建的图挂了,于是枚举了几种建图方式-- 因为要删点,所以拆点,连接(i,i',1),对于原来图上的边(u,v),连接(u',v,inf),(v',u,inf),然后连接(s,i ...
- noip2002矩阵覆盖(搜索)
矩阵覆盖 题目描述 在平面上有 n 个点(n <= 50),每个点用一对整数坐标表示.例如:当 n=4 时,4个点的坐标分另为:p1(1,1),p2(2,2),p3(3,6),P4(0,7),见 ...
- redis在linux安装和开机启动和结合php运用方法一
第一部分:安装redis 希望将redis安装到此目录 1 /usr/local/redis 希望将安装包下载到此目录 1 /usr/local/src 那么安装过程指令如下: 1 2 3 4 5 6 ...
- JavaScript--History 对象
history对象记录了用户曾经浏览过的页面(URL),并可以实现浏览器前进与后退相似导航的功能. 注意:从窗口被打开的那一刻开始记录,每个浏览器窗口.每个标签页乃至每个框架,都有自己的history ...
- Html5 编程题
1.请写出下面所示的控件的html 代码? <div> <div><a>姓名:</a><input type="text" ...
- document.write()、onclick="alert(xxx)、innerHTML、image.src.match("xxx")、id2.style.color="blue";、isNaN(id2)、document.write("糟糕!文档消失了。")、alert(id2.outerHTML)、id2.className="id06";、onclick="return registe"
<html> <head> <meta charset="utf-8"> <title>javascript</title&g ...
- 为什么,博主我要写下这一系列windows实用网络?
发现,随着自身一路过来所学,无论在大数据领域.还是linux or windows里,菜鸟的我慢慢在长大.把自己比作一个园,面积虽在增加,涉及面增多,但圆外的东西,还是那么多. 现在,正值在校读研 ...
- EasyUI系列学习(八)-ProgressBar(进度条)
一.创建组件 1.class加载 <div class="easyui-progressbar"></div> 2.js加载 <div id=&quo ...