D. Design Tutorial: Inverse the Problem
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

There is an easy way to obtain a new task from an old one called "Inverse the problem": we give an output of the original task, and ask to generate an input, such that solution to the original problem will produce the output we provided. The hard task of Topcoder Open 2014 Round 2C, InverseRMQ, is a good example.

Now let's create a task this way. We will use the task: you are given a tree, please calculate the distance between any pair of its nodes. Yes, it is very easy, but the inverse version is a bit harder: you are given an n × n distance matrix. Determine if it is the distance matrix of a weighted tree (all weights must be positive integers).

Input

The first line contains an integer n (1 ≤ n ≤ 2000) — the number of nodes in that graph.

Then next n lines each contains n integers di, j (0 ≤ di, j ≤ 109) — the distance between node i and node j.

Output

If there exists such a tree, output "YES", otherwise output "NO".

Sample test(s)
input
3
0 2 7
2 0 9
7 9 0
output
YES
input
3
1 2 7
2 0 9
7 9 0
output
NO
input
3
0 2 2
7 0 9
7 9 0
output
NO
input
3
0 1 1
1 0 1
1 1 0
output
NO
input
2
0 0
0 0
output
NO
 
给出一个矩阵,表示两个点之间的距离,判断这些点能否构成一颗树。。

1th:构造最小生成树。

我们提取所有的边出来按边排序,因为每次我们知道边的权值>0,

之后每次把边加入集合中,不断构造,类似  kruskal算法,构造出边后

再对每个点进行整张图的DFS求距离

复杂度O(N^2lgN):对所有边排序的复杂度。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std; int n;
int nu[][];
int fa[];
int ct = ; struct edge
{
int x,y,w;
edge(int x = ,int y = ,int w = ):x(x),y(y),w(w){}
bool operator <(const edge &rhs) const
{
return w<rhs.w;
}
}eg[*]; vector<int> g[]; int find(int x) {
if(x==fa[x]) return x;
else return fa[x] = find(fa[x]);
} int node;
int flag= ; int kruskal()
{
sort(eg,eg+ct);
for(int i = ;i<n;i++) fa[i] = i;
for(int i = ;i<ct;i++)
{
int tx = find(eg[i].x);
int ty = find(eg[i].y);
if(tx!=ty)
{
fa[tx] = ty;
g[eg[i].x].push_back(eg[i].y);
g[eg[i].y].push_back(eg[i].x);
//cout<<eg[i].x<<' '<<eg[i].y<<' '<<eg[i].w<<'?'<<endl;
}
}
} int dfs(int v,int now,int fa)
{
for(int i = ;i<g[v].size();i++)
{
int t = g[v][i];
if(t == fa) continue;
//cout<<node<<' '<<t<<' '<<nu[v][t]+now<<endl;
int wt = nu[v][t]+now;
if(nu[node][t]!=wt) {flag = ;return -;}
else
dfs(t,wt,v);
}
return ;
} int main()
{
scanf("%d",&n);
int num;
for(int i = ;i<n;i++) g[i].clear();
for(int i = ;i<n;i++)
for(int j = ;j<n;j++)
{
scanf("%d",&num);
nu[i][j] = num;
if(i>j)
{
eg[ct].x = i;
eg[ct].y = j;
eg[ct++].w = num;
}
if(nu[i][j]!=&&i==j) flag = ;
if(j<i&&nu[i][j]!=nu[j][i]) flag = ;
if(nu[i][j]==&&i!=j) flag = ;
}
if(flag!=)
{
kruskal();
for(int i = ;i<n;i++)
{
node = i;
if(dfs(i,,-)==-) {flag = ; break;}
}
}
if(flag == ) puts("NO");
else puts("YES");
return ;
}
/*
7
0 1 1 2 2 3 3
1 0 2 1 3 2 4
1 2 0 3 1 4 2
2 1 3 0 4 1 5
2 3 1 4 0 5 1
3 2 4 1 5 0 6
3 4 2 5 1 6 0
*/

第二种做法不得不佩服!1

这个特种点很难在比赛中发现啊

关键点:我们知道一个点到其他所有点都有一条最小距离的边--假设A到其他点最小距离的点是B,A-B一定是直接连接的。假设距离是X。

然后假设一个点C,C到B点要么比C到A点近X,要么C到A点远X。具体可以画图。

通过这个方法可以判断数据是否合法!

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std; int num[][];
int n; int main()
{
scanf("%d",&n);
for(int i = ;i<n;i++)
for(int j = ;j<n;j++)
{
scanf("%d",&num[i][j]);
}
int flag = ;
for(int i = ;i<n;i++)
for(int j = i;j<n;j++)
{
if(num[i][j]!=&&i==j) flag = ;
if(num[i][j]!=num[j][i]) flag = ;
if(num[i][j]==&&i!=j) flag = ;
} for(int i = ;i<n;i++)
{
int inf = ;
int pos = -;
for(int j = ;j<n;j++)
{
if(i==j) continue;
if(num[i][j]<inf)
{
inf = num[i][j];
pos = j;
}
} for(int j = ;j<n;j++)
{
if(j==i||j==pos) continue;
if(abs(num[i][j]-num[pos][j])!=inf)
flag = ;
}
}
if(flag == ) puts("NO");
else puts("YES");
return ;
}

Codeforces #270 D. Design Tutorial: Inverse the Problem的更多相关文章

  1. Codeforces Round #270 D Design Tutorial: Inverse the Problem --MST + DFS

    题意:给出一个距离矩阵,问是不是一颗正确的带权树. 解法:先按找距离矩阵建一颗最小生成树,因为给出的距离都是最短的点间距离,然后再对每个点跑dfs得出应该的dis[][],再对比dis和原来的mp是否 ...

  2. cf472D Design Tutorial: Inverse the Problem

    D. Design Tutorial: Inverse the Problem time limit per test 2 seconds memory limit per test 256 mega ...

  3. D. Design Tutorial: Inverse the Problem 解析含快速解法(MST、LCA、思維)

    Codeforce 472D Design Tutorial: Inverse the Problem 解析含快速解法(MST.LCA.思維) 今天我們來看看CF472D 題目連結 題目 給你一個\( ...

  4. codeforces D. Design Tutorial: Inverse the Problem

    题意:给定一个矩阵,表示每两个节点之间的权值距离,问是否可以对应生成一棵树, 使得这棵树中的任意两点之间的距离和矩阵中的对应两点的距离相等! 思路:我们将给定的矩阵看成是一个图,a 到 b会有多条路径 ...

  5. Design Tutorial: Inverse the Problem

    Codeforces Round #270 D:http://codeforces.com/contest/472/problem/D 题意:给以一张图,用邻接矩阵表示,现在问你这张图能不能够是一棵树 ...

  6. 【CF】270D Design Tutorial: Inverse the Problem

    题意异常的简单.就是给定一个邻接矩阵,让你判定是否为树.算法1:O(n^3).思路就是找到树边,原理是LCA.判断树边的数目是否为n-1.39-th个数据T了,自己测试2000跑到4s.算法2:O(n ...

  7. Codeforces Round #270--B. Design Tutorial: Learn from Life

    Design Tutorial: Learn from Life time limit per test 1 second memory limit per test 256 megabytes in ...

  8. codeforces水题100道 第七题 Codeforces Round #270 A. Design Tutorial: Learn from Math (math)

    题目链接:http://www.codeforces.com/problemset/problem/472/A题意:给你一个数n,将n表示为两个合数(即非素数)的和.C++代码: #include & ...

  9. Codeforces Round #270 A. Design Tutorial: Learn from Math【数论/埃氏筛法】

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

随机推荐

  1. POJ 题目分类(转载)

    Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...

  2. [IOS多线程]的使用:防止进行HTTP数据请求时,UI卡死。

    多线程的实现:NSThread 1.子线程的创建:两种方法 第一种: [NSThread detachNewThreadSelector:@selector(downloadImage:) toTar ...

  3. vc++ 中 IntelliSense: 无法打开 源 文件 "xxx.h"

    类似无法找到文件的问题都可以用这个方法解决,就是路径的问题.vc++2008的项目转到vc++2010也可能出现类似的问题. 解决方法: 在  项目属性=>配置属性=>C/C++  =&g ...

  4. 【Alpha版本】 第五天 11.11

    一.站立式会议照片: 二.项目燃尽图: 三.项目进展: 成 员 昨天完成任务 今天完成任务 周末+下周一要做任务 问题困难 心得体会 胡泽善 完成了账户信息修改界面 完成管理员的三大界面框架.完成管理 ...

  5. 4 构建Mysql+heartbeat+DRBD+LVS集群应用系统系列之Lvs为Mysql-slave做负载均衡

    preface Mysql+drbd+heart能够实现Mysql的高可用了,master出现故障的时候能够快速切换.在现在的业务情况下,读操作多,写操作少的情况下,一台DB server明显扛不住, ...

  6. swfUpload 上传图片

    前端: <script src="~/Scripts/swfupload/swfupload.js"></script> <script src=&q ...

  7. 关于主机FTP连接不上,无法列出目录,列表错误,上传速度慢,掉速的解决办法

    FTP是一种文件传输协议,它支持两种模式: 一种方式叫做Standard (也就是 Active,主动方式), 一种是 Passive (也就是PASV,被动方式). Standard模式 FTP的客 ...

  8. easyUI datagrid学习笔记

    1.easyUI表格的列属性 formatter:function(value,rowdata,rowindex) { return '['+value+']';//格式化,给每个值加上'[]': } ...

  9. java实现Haffman编码

    1.先创建一个树节点类(泛型类),为了方便使用集合的排序方法,泛型类要实现泛型接口Comparable,代码如下 package com.hjp.huffman; /** * Created by J ...

  10. 用jackson封装的JSON工具类

    package hjp.smart4j.framework.util; import com.fasterxml.jackson.databind.ObjectMapper; import org.s ...