Codeforces 472D. Design Tutorial: Inverse the Problem(一种逆向判定树成立的办法,从Kruskal到dfs)

来源:互联网 发布:python pyperclip 编辑:程序博客网 时间:2024/06/06 14:18

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
30 2 72 0 97 9 0
output
YES
input
31 2 72 0 97 9 0
output
NO
input
30 2 27 0 97 9 0
output
NO
input
30 1 11 0 11 1 0
output
NO
input
20 00 0
output
NO
Note

In the first example, the required tree exists. It has one edge between nodes 1 and 2 with weight 2, another edge between nodes 1 and 3 with weight 7.

In the second example, it is impossible because d1, 1 should be 0, but it is 1.

In the third example, it is impossible because d1, 2 should equal d2, 1.


题目地址:http://codeforces.com/contest/472/problem/D

先特判,不存在特判情况就,构成半个完全图,然后跑一发最小生成树里的Kruskal算法,同时构成一颗无向的树

然后枚举起点,跑n发dfs搜索,判断是否连通并且判断题目中给定的矩阵是否满足这棵树。就没了

这题,能省就省,用int,别用longlong,然后,循环能少点就少点,系数能少就少,否则会T的


//Hello. I'm Peter.#include<cstdio>#include<iostream>#include<sstream>#include<cstring>#include<string>#include<cmath>#include<cstdlib>#include<algorithm>#include<functional>#include<cctype>#include<ctime>#include<stack>#include<queue>#include<vector>#include<set>#include<map>using namespace std;typedef long long ll;typedef long double ld;#define peter cout<<"i am peter"<<endl#define input freopen("data.txt","r",stdin)#define randin srand((unsigned int)time(NULL))#define INT (0x3f3f3f3f)*2#define LL (0x3f3f3f3f3f3f3f3f)*2#define gsize(a) (int)a.size()#define len(a) (int)strlen(a)#define slen(s) (int)s.length()#define pb(a) push_back(a)#define clr(a) memset(a,0,sizeof(a))#define clr_minus1(a) memset(a,-1,sizeof(a))#define clr_INT(a) memset(a,INT,sizeof(a))#define clr_true(a) memset(a,true,sizeof(a))#define clr_false(a) memset(a,false,sizeof(a))#define clr_queue(q) while(!q.empty()) q.pop()#define clr_stack(s) while(!s.empty()) s.pop()#define rep(i, a, b) for (int i = a; i < b; i++)#define dep(i, a, b) for (int i = a; i > b; i--)#define repin(i, a, b) for (int i = a; i <= b; i++)#define depin(i, a, b) for (int i = a; i >= b; i--)#define pi 3.1415926535898#define eps 1e-6#define MOD 1000000007#define MAXN 4001000#define N 2014#define M 100100void printno();void printyes();int n;int d[N][N];bool ok;struct Complete_Graph{    int to,from;    int val;}comedge[MAXN];int comw=0;void add_comedge(int from,int to,int val){    int t=++comw;    comedge[t].from=from;    comedge[t].to=to;    comedge[t].val=val;}bool comp(const Complete_Graph a,const Complete_Graph b){    return a.val<b.val;}int father[N];int find_father(int x){    if(father[x]!=x) father[x]=find_father(father[x]);    return father[x];}struct Edge{    int to,from,next;    int val;}edge[M];int head[N],w;void add_edge(int from,int to,int val){    w++;    edge[w].from=from;    edge[w].to=to;    edge[w].val=val;    edge[w].next=head[from];    head[from]=w;}void Kruskal(){//经典的Kruskal算法,最小生成树,同时构建最小生成树的无向图,要用到并查集    sort(comedge+1,comedge+1+comw,comp);    repin(i,1,n)    {        father[i]=i;    }    int from,to;    int val;    clr_minus1(head);    w=0;    repin(i,1,comw)    {        from=comedge[i].from;        to=comedge[i].to;        val=comedge[i].val;        int fa1=find_father(from);        int fa2=find_father(to);        if(fa1!=fa2)        {            father[fa2]=fa1;            add_edge(from,to,val);            add_edge(to,from,val);        }    }}bool vis[N];int dis[N];void dfs(int now,int val){    vis[now]=true;    dis[now]=val;    int i,to;    for(i=head[now];i!=-1;i=edge[i].next)    {        to=edge[i].to;        if(vis[to]) continue;        dfs(to,val+edge[i].val);    }}int main(){    cin>>n;    repin(i,1,n)    {        repin(j,1,n)        {            scanf("%d",&d[i][j]);        }    }    ok=true;    repin(i,1,n)    {        if(!ok) break;        repin(j,i,n)        {            if(i==j && d[i][j]!=0) ok=false;            else if(i!=j && (d[i][j]==0 || d[j][i]==0 || d[i][j]!=d[j][i])) ok=false;            add_comedge(i,j,d[i][j]);        }    }    printno();    if(n==1) printyes();    //当不存在特判情况后,跑一发Kruskal算法    Kruskal();    //跑完后,跑n发dfs搜索,判断给定的矩阵是否满足这棵树    repin(i,1,n)    {        if(!ok) break;        repin(j,1,n)        {            vis[j]=false;        }        dfs(i,0);        repin(j,1,n)        {            if(!vis[j] || dis[j]!=d[i][j])            {                ok=false;                break;            }        }    }    printno();    printyes();}void printno(){    if(!ok)    {        printf("NO\n");        exit(0);    }}void printyes(){    if(ok)    {        printf("YES\n");        exit(0);    }}


0 0
原创粉丝点击