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 ...
随机推荐
- MapInfo 文件格式说明
MapInfo 文件格式说明(id.map.tab.dat) (1). 属性数据的表结构文件.TAB 属性数据表结构文件定义了地图属性数据的表结构,包括字段数.字段名称.字段类型和字段宽度.索引字段及 ...
- 47求1+2+3+...+n
题目描述 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 用递归 public class Soluti ...
- iOS 什么是函数式编程
前言:当前只做理解性的常规背书,根据不断深入学习会不断丰富解读内容,欢迎评论提意见 函数式编程:Functional Programming 1 基本解释: 函数式编程 是一种思维模式,一种编程思想, ...
- Service Fusing
服务熔断也称服务隔离,来自于Michael Nygard 的<Release It>中的CircuitBreaker应用模式,Martin Fowler在博文CircuitBreaker中 ...
- 每天一个Linux命令(48)ping命令
ping命令用来测试主机之间网络的连通性. (1)用法: 用法: ping [参数] [主机名或IP地址] (2)功能: 功能: 确定网络和各外部主机的状态 ...
- Qt5.2.1交叉编译,带tslib插件
一: 源码下载地址: 1.1: 平台: 主机:ubuntu 14.04 开发板: cpu arm-cortex-a8,故而我在配置我的qmake.conf的时候填写的为armV7-a QT版本: qt ...
- blast+简介
blast+有三大工具类型: 功能 search database filter 命令 blastn, blastp, blastx, tblastx, tblastn, psiblast, rpsb ...
- Go 功能测试与性能测试
1.功能测试 calcTriangle.go // 需要被测试的函数 func calcTriangle(a, b int) int { return int(math.Sqrt(float64(a* ...
- 九度oj-题目1103:二次方程计算器
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2799 解决:633 题目描述: 设计一个二次方程计算器 输入: 每个案例是关于x的一个二次方程表达式,为了简单,每个系数都是整数形式. 输 ...
- CCNA 课程 三
交换机的MAC地址学习情况: 1.从一个接口收到数据帧,根据数据帧的原mac地址查找交换机的mac地址表,如果没有找到,将会添加数据帧的原mac地址和收到数据帧接口的对应条目,放进交换机的mac地址表 ...