Codeforces Round #277.5 (Div. 2)D Unbearable Controversy of Being (暴力)
2014-11-18 20:25
260 查看
这道题我临场想到了枚举菱形的起点和终点,然后每次枚举起点指向的点,每个指向的点再枚举它指向的点看有没有能到终点的,有一条就把起点到终点的路径个数加1,最后ans+=C(路径总数,2)。每两个点都这么弄。但是我考虑时间复杂度n2前面的系数过大会超时,再想别的方法也没想出来。。
其实思路就是这样的,只不过时间上可以优化一下,就是用常用的两种做法不变而优化时间复杂度的方法:1.以空间换时间2.分步降维
分步降维
其实思路就是这样的,只不过时间上可以优化一下,就是用常用的两种做法不变而优化时间复杂度的方法:1.以空间换时间2.分步降维
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<algorithm> #include<stack> #include<queue> #include<cctype> #include<sstream> using namespace std; #define pii pair<int,int> #define LL long long int const int eps=1e-8; const int INF=1000000000; const int maxn=3000+10; vector<int>v[maxn]; int n,m,a,b,num[maxn][maxn],ans=0; int main() { //freopen("in8.txt","r",stdin); //freopen("out.txt","w",stdout); scanf("%d%d",&n,&m); for(int i=0;i<m;i++) { scanf("%d%d",&a,&b); v[a].push_back(b); } for(int i=1;i<=n;i++) { int si=v[i].size(); for(int j=0;j<si;j++) { int t=v[i][j]; int st=v[t].size(); for(int k=0;k<st;k++) { num[i][v[t][k]]++; } } } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if((i!=j)&&(num[i][j]>0)) { ans+=(num[i][j]-1)*num[i][j]/2; } } } printf("%d\n",ans); //fclose(stdin); //fclose(stdout); return 0; }
分步降维
相关文章推荐
- Codeforces Round #277.5(Div. 2) D. Unbearable Controversy of Being【暴力】
- Codeforces Round #277.5 (Div. 2)-D. Unbearable Controversy of Being
- Codeforces Round #277.5 (Div. 2) D. Unbearable Controversy of Being
- Codeforces Round #277.5 (Div. 2) D——Unbearable Controversy of Being
- Codeforces Round #277.5 (Div. 2) D - Unbearable Controversy of
- Codeforces Round #398(Div. 2)D. Cartons of milk【二分+暴力处理】
- Codeforces Round #277.5(Div. 2) C. Given Length and Sum of Digits...【贪心】
- Unbearable Controversy of Being CodeForces - 489D
- Codeforces Round #277.5 (Div. 2)-C. Given Length and Sum of Digits...
- Codeforces Round #277.5 (Div. 2)C. Given Length and Sum of Digits...(贪心)
- Codeforces Round #277.5 (Div. 2)——C贪心—— Given Length and Sum of Digits
- Codeforces Round #277.5 (Div. 2)C. Given Length and Sum of Digits...(贪心)
- Codeforces Round #277.5 (Div. 2) C. Given Length and Sum of Digits...
- Codeforces Round #277.5 (Div. 2)C——Given Length and Sum of Digits...
- Codeforces Round #277.5 (Div. 2)C——Given Length and Sum of Digits...
- Codeforces Round VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM 暴力出奇迹!
- Codeforces Round #277.5 (Div. 2) C. Given Length and Sum of Digits...
- Codeforces Round #345 (Div. 2) D. Image Preview 暴力 二分
- Educational Codeforces Round 34 (Rated for Div. 2) E. Swapping Characters(暴力)
- Codeforces Round #277.5 (Div. 2) F