dfs and similar dsu shortest paths trees *1900

Please click on ads to support us..

C++ Code:

// Problem: D. Design Tutorial: Inverse the Problem
// Contest: Codeforces - Codeforces Round #270
// URL: https://codeforces.com/contest/472/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<utility>
#include<set>
#include<unordered_set>
#include<list>
#include<iterator>
#include<deque>
#include<queue>
#include<stack>
#include<set>
#include<bitset>
#include<random>
#include<map>
#include<unordered_map>
#include<stdio.h>
#include<complex>
#include<math.h>
#include<cstring>
#include<chrono>
#include<string>

#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("fast-math") 

using namespace std;
#define pb push_back
#define ff first
#define ss second
#define vi vector <int>
#define vvi vector <vi>
#define pii pair<int, int>
#define ppi pair<int, pii>
#define vii vector<pii>
#define mii map<int, int>
#define mci map<char, int>
#define miv map<int, vi>
#define mis map<int, set<int>>
#define setbits(n) __builtin_popcount(n)
#define all(v) (v).begin(), (v).end()
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define endl "\n"
#define fo(i,n) for(int i=0;i<n;i++)
#define in(a,n)	for(int i=0;i<n;i++) cin>>a[i];
#define show2(a, b) cout<<a<<' '<<b<<endl
#define show3(a, b, c) cout<<a<<' '<<b<<' '<<c<<endl
#define show(arr) for (auto i:arr) cout<<i<<' '; 
#define Endl endl
const long long N=1e5+5;
const long long mod=1000000007; //998244353;

int n,cnt;
int d[2001][2001],par[2001],rnk[2001];
void make_set(int n)
{
	for(int i=1;i<=n;i++)
	{
		rnk[i]=1;par[i]=i;
	}
}
int find_set(int u)
{
	return (par[u]==u)?u:par[u]=find_set(par[u]);
}
void union_set(int u, int v)
{
	int p=par[u],q=par[v];
	if(p==q)return;
	if(rnk[p]<rnk[q])swap(p,q);
	par[q]=p;rnk[p]+=rnk[q];
}
vector<ppi> edges;
vi adj[2001];
bool dfs(int u, int p, int s, int x)
{
	if(d[s][u]!=x)return false;
	bool res=true;
	for(auto v:adj[u])
	{
		if(v==p)continue;
		res=res&dfs(v,u,s,x+d[u][v]);
	}
	return res;
}
void cases(){
	cin>>n;cnt=n;
	for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)cin>>d[i][j];
	for(int i=1;i<=n;i++)
	{
		for(int j=i+1;j<=n;j++)
		{
			if(d[i][j]==0 || d[i][j]!=d[j][i])
			{
				no;return;
			}
			edges.pb({d[i][j],{i,j}});
		}
		if(d[i][i])
		{
			no;return;
		}
	}
	sort(all(edges));
	make_set(n);
	int sz=edges.size();
	for(int i=0;i<sz;i++)
	{
		int u=edges[i].ss.ff,v=edges[i].ss.ss;
		if(cnt==1)break;
		if(find_set(u)!=find_set(v))
		{
			adj[u].pb(v);adj[v].pb(u);union_set(u,v);
			cnt--;
		}
	}
	bool f=1;
	for(int i=1;i<=n;i++)
	{
		f=f&dfs(i,-1,i,0);
	}
	f?yes:no;
	
}

int32_t main(){
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    int t=1;
    // cin>>t;
    for (int i=0; i<t; i++){
    	//cout<<"Case #"<<i+1<<": ";
		cases();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

448A - Rewards
1622A - Construct a Rectangle
1620A - Equal or Not Equal
1517A - Sum of 2050
620A - Professor GukiZ's Robot
1342A - Road To Zero
1520A - Do Not Be Distracted
352A - Jeff and Digits
1327A - Sum of Odd Integers
1276A - As Simple as One and Two
812C - Sagheer and Nubian Market
272A - Dima and Friends
1352C - K-th Not Divisible by n
545C - Woodcutters
1528B - Kavi on Pairing Duty
339B - Xenia and Ringroad
189A - Cut Ribbon
1182A - Filling Shapes
82A - Double Cola
45A - Codecraft III
1242A - Tile Painting
1663E - Are You Safe
1663D - Is it rated - 3
1311A - Add Odd or Subtract Even
977F - Consecutive Subsequence
939A - Love Triangle
755A - PolandBall and Hypothesis
760B - Frodo and pillows
1006A - Adjacent Replacements
1195C - Basketball Exercise