zoukankan      html  css  js  c++  java
  • 【cf489】D. Unbearable Controversy of Being(暴力)

    http://codeforces.com/contest/489/problem/D

    很显然,我们只需要找对于每个点能到达的深度为3的点的路径的数量,那么对于一个深度为3的点,如果有a种方式到达,那么有方案数(a-1+1)*(a-1)/2

    可是我用dfs找路径就tle了QAQ

    于是orz别人的代码,,,,是暴力。。。。。。。。。。。。。。。。。。。。。。。。直接两重循环orz

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <string>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <set>
    #include <map>
    using namespace std;
    typedef long long ll;
    #define rep(i, n) for(int i=0; i<(n); ++i)
    #define for1(i,a,n) for(int i=(a);i<=(n);++i)
    #define for2(i,a,n) for(int i=(a);i<(n);++i)
    #define for3(i,a,n) for(int i=(a);i>=(n);--i)
    #define for4(i,a,n) for(int i=(a);i>(n);--i)
    #define CC(i,a) memset(i,a,sizeof(i))
    #define read(a) a=getint()
    #define print(a) printf("%d", a)
    #define dbg(x) cout << (#x) << " = " << (x) << endl
    #define error(x) (!(x)?puts("error"):0)
    #define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
    inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
    const int N=3005;
    struct dat { int to, next; }e[N*10];
    int cnt, vis[N], c[N], n, m, ihead[N];
    void add(int u, int v) { e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v; }
    void bfs(int x, int dep) {
    	rdm(x, i) {
    		int y=e[i].to;
    		rdm(y, j) {
    			int z=e[j].to;
    			if(x==z) continue;
    			++c[z];
    		}
    	}
    }
    ll ans;
    int main() {
    	read(n); read(m);
    	for1(i, 1, m) { int u=getint(), v=getint(); add(u, v); }
    	for1(i, 1, n) {
    		for1(j, 1, n) vis[j]=0, c[j]=0;
    		bfs(i, 1);
    		//for1(j, 1, n) cout << c[j] << ' '; cout << endl;
    		for1(j, 1, n) if(c[j]>=2) {
    			--c[j];
    			ans+=(ll)(c[j]+1)*c[j]/2;
    		}
    	}
    	printf("%I64d
    ", ans);
    	return 0;
    }
    

      


    Tomash keeps wandering off and getting lost while he is walking along the streets of Berland. It's no surprise! In his home town, for any pair of intersections there is exactly one way to walk from one intersection to the other one. The capital of Berland is very different!

    Tomash has noticed that even simple cases of ambiguity confuse him. So, when he sees a group of four distinct intersections a, b, c and d, such that there are two paths from a to c — one through b and the other one through d, he calls the group a "damn rhombus". Note that pairs (a, b), (b, c), (a, d), (d, c) should be directly connected by the roads. Schematically, a damn rhombus is shown on the figure below:

    Other roads between any of the intersections don't make the rhombus any more appealing to Tomash, so the four intersections remain a "damn rhombus" for him.

    Given that the capital of Berland has n intersections and m roads and all roads are unidirectional and are known in advance, find the number of "damn rhombi" in the city.

    When rhombi are compared, the order of intersections b and d doesn't matter.

    Input

    The first line of the input contains a pair of integers n, m (1 ≤ n ≤ 3000, 0 ≤ m ≤ 30000) — the number of intersections and roads, respectively. Next m lines list the roads, one per line. Each of the roads is given by a pair of integers ai, bi (1 ≤ ai, bi ≤ n;ai ≠ bi) — the number of the intersection it goes out from and the number of the intersection it leads to. Between a pair of intersections there is at most one road in each of the two directions.

    It is not guaranteed that you can get from any intersection to any other one.

    Output

    Print the required number of "damn rhombi".

    Sample test(s)
    input
    5 4
    1 2
    2 3
    1 4
    4 3
    output
    1
    input
    4 12
    1 2
    1 3
    1 4
    2 1
    2 3
    2 4
    3 1
    3 2
    3 4
    4 1
    4 2
    4 3
    output
    12
  • 相关阅读:
    Educational Codeforces Round 3 B
    Educational Codeforces Round 3 A
    BestCoder Round #66 1002
    Codeforces Round #335 (Div. 2) B
    BestCoder Round #66 1001
    Codeforces Round #335 (Div. 2) C
    Linux加载DTS设备节点的过程(以高通8974平台为例)
    高通APQ8074 spi 接口配置
    摩托罗拉SE4500 德州仪器TI Omap37xx/AM3715/DM3730/AM3530 wince6.0/Windows Mobile 6.5平台 二维软解调试记录及相关解释
    摩托罗拉SE4500 三星 S3C6410 Wince6.0平台软解码调试记录以及驱动相关问题解释
  • 原文地址:https://www.cnblogs.com/iwtwiioi/p/4136447.html
Copyright © 2011-2022 走看看