Codeforces Round #277.5 (Div. 2)D Unbearable Controversy of Being (暴力)
这道题我临场想到了枚举菱形的起点和终点,然后每次枚举起点指向的点,每个指向的点再枚举它指向的点看有没有能到终点的,有一条就把起点到终点的路径个数加1,最后ans+=C(路径总数,2)。每两个点都这么弄。但是我考虑时间复杂度n2前面的系数过大会超时,再想别的方法也没想出来。。
其实思路就是这样的,只不过时间上可以优化一下,就是用常用的两种做法不变而优化时间复杂度的方法:1.以空间换时间2.分步降维
#include <cstdio>
#include <vector> using namespace std; const int maxn = + ;
bool G[maxn][maxn];
vector<int> nxt[maxn]; int main()
{
//freopen("in.txt", "r", stdin);
int n, m;
scanf("%d%d", &n, &m);
for(int i = ; i < m; ++i)
{
int a, b;
scanf("%d%d", &a, &b);
G[a][b] = true;
nxt[a].push_back(b);
} int ans = ;
for(int a = ; a <= n; ++a)
for(int c = ; c <= n; ++c)
{
if(a != c)
{
int r = ;
for(int b = ; b < nxt[a].size(); ++b)
{
if(nxt[a][b] != a && nxt[a][b] != c && G[nxt[a][b]][c])
r++;
}
ans += r*(r-)/;
}
} printf("%d\n", ans); return ;
}
以空间换时间
#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-;
const int INF=;
const int maxn=+;
vector<int>v[maxn];
int n,m,a,b,num[maxn][maxn],ans=;
int main()
{
//freopen("in8.txt","r",stdin);
//freopen("out.txt","w",stdout);
scanf("%d%d",&n,&m);
for(int i=;i<m;i++)
{
scanf("%d%d",&a,&b);
v[a].push_back(b);
}
for(int i=;i<=n;i++)
{
int si=v[i].size();
for(int j=;j<si;j++)
{
int t=v[i][j];
int st=v[t].size();
for(int k=;k<st;k++)
{
num[i][v[t][k]]++;
}
}
}
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if((i!=j)&&(num[i][j]>))
{
ans+=(num[i][j]-)*num[i][j]/;
}
}
}
printf("%d\n",ans);
//fclose(stdin);
//fclose(stdout);
return ;
}
分步降维
Codeforces Round #277.5 (Div. 2)D Unbearable Controversy of Being (暴力)的更多相关文章
- Codeforces Round #277.5 (Div. 2)-D. Unbearable Controversy of Being
http://codeforces.com/problemset/problem/489/D D. Unbearable Controversy of Being time limit per tes ...
- Codeforces Round #277.5 (Div. 2) ABCDF
http://codeforces.com/contest/489 Problems # Name A SwapSort standard input/output 1 s, 256 ...
- Codeforces Round #277.5 (Div. 2)
题目链接:http://codeforces.com/contest/489 A:SwapSort In this problem your goal is to sort an array cons ...
- Codeforces Round #277.5 (Div. 2) A,B,C,D,E,F题解
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud A. SwapSort time limit per test 1 seco ...
- Codeforces Round #277.5 (Div. 2)-D
题意:求该死的菱形数目.直接枚举两端的点.平均意义每一个点连接20条边,用邻接表暴力计算中间节点数目,那么中间节点任选两个与两端可组成的菱形数目有r*(r-1)/2. 代码: #include< ...
- Codeforces Round #277.5 (Div. 2)部分题解
A. SwapSort time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- Codeforces Round #277.5 (Div. 2) --E. Hiking (01分数规划)
http://codeforces.com/contest/489/problem/E E. Hiking time limit per test 1 second memory limit per ...
- Codeforces Round #277.5 (Div. 2)-C. Given Length and Sum of Digits...
http://codeforces.com/problemset/problem/489/C C. Given Length and Sum of Digits... time limit per t ...
- Codeforces Round #277.5 (Div. 2)-B. BerSU Ball
http://codeforces.com/problemset/problem/489/B B. BerSU Ball time limit per test 1 second memory lim ...
随机推荐
- LeetCode-day04
35. Same Tree 判断两棵树相同 36. Invert Binary Tree 翻转树 37. Symmetric Tree 判断树是不是镜像对称的 38. Maximum Depth of ...
- 系统非正常关机启动后出现:an error occurred during the file system
现象描述: 1.系统ssh登录报Too many open files in system,系统登录不进去,就直接强制关机了,开机后出现(2)的错误: 由于文件描述符用完了,需要把fs.file-ma ...
- 小数据池、is 和 ==的区别
小数据池,在一定情况下出现内存共享(只有int 和 str 才有的) is 和 ==的区别 id() 打印数据的地址 a = 'hello' b = 'hello' print(a = ...
- java中类名.class, class.forName(), getClass()区别
Class对象的生成方式如下: 1.类名.class 说明: JVM将使用类装载器, 将类装入内存(前提是:类还没有装入内存),不做类的初始化工作.返回Class的对象 2.Cla ...
- [转]springmvc中的常用的返回
package com.boventech.learning.controller; import java.util.HashMap; import java.util.Map; import or ...
- 基于linux的ekho(余音)安装与开发
Ekho(余音)是一个把文字转换成声音的软件.它目前支持粤语.普通话(国语).诏安客语和韩语(试验中),英文则通过Festival间接实现.它比eSpeak的设计更简易,但文件较大.由于使用了真人发声 ...
- java中,return和return null有什么区别吗?
java中,return和return null有什么区别吗? 最大的区别:return;方法的返回值必须是void!return null;方法的返回值必须不是 原始数据类型(封装类除过)和void ...
- this和super用法
1. this能分清混淆,形参名与当前对象的某个成员有相同的名字,需要明确使用this关键字来指明你要使用某个成员,使用方法是“this.成员名”. 一般以this.形参数名=形参名,代表送进来赋值的 ...
- MAC 系列 之XCode7.1 + HBuilder MUI 离线打包 ipa 上次application leader 问题:ERROR ITMS - 90534
解决方法:这个原因 网上说法是 beta 测试版本:不过的确是beta版本(7.3 beta)打包的,所以我有下载了一个正式版本 7.1版本. 再次进行测试打包!
- Python之print语句Python的注释
话不多说直接切入正题 print语句可以向屏幕上输出指定的文字.比如输出'hello, world',用代码实现如下: >>> print 'hello, world' 注意: 1. ...