AGC038D - Unique Path(建图)

AGC038D - Unique Path

Solution

此题较水。
大概就是简单路径唯一意味着连成一棵树,因此先给这些限制的端点放在同一个连通块内,然后如果有多条路径的限制的两端点在同一个连通块内,则无解。

然后考虑如果没有多条路径的限制,则还要连的边数最少为 t − 1 t-1 t1,最多为 t ( t − 1 ) 2 \frac{t(t-1)}{2} 2t(t1) t t t为连通块个数),直接判断即可。

如果有多条路径,则还要连的边数最少为 t t t(一种可行方案是每个限制以以每个连通块的根为端点连成环),最多为 t ( t − 1 ) 2 \frac{t(t-1)}{2} 2t(t1),且连通块个数至少要有两个,否则没法连成环。

时间复杂度 O ( n l g n ) O(nlgn) O(nlgn)

Code

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cassert>
#include <string.h>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>

#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se second

using namespace std;

template<typename T>inline bool upmin(T &x,T y) { return y<x?x=y,1:0; }
template<typename T>inline bool upmax(T &x,T y) { return x<y?x=y,1:0; }

typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;

const lod eps=1e-11;
const lod pi=acos(-1);
const int oo=1<<30;
const ll loo=1ll<<62;
const int mods=998244353;
const int MAXN=600005;
const int INF=0x3f3f3f3f;//1061109567
/*--------------------------------------------------------------------*/
inline ll read()
{
	ll f=1,x=0; char c=getchar();
	while (c<'0'||c>'9') { if (c=='-') f=-1; c=getchar(); }
	while (c>='0'&&c<='9') { x=(x<<3)+(x<<1)+(c^48); c=getchar(); }
	return x*f;
}
int f[MAXN];
struct Enode{ int u,v,c; } E[MAXN];
int find(int x) { return f[x]==x?f[x]:f[x]=find(f[x]); }
signed main()
{
	ll n=read(),m=read(),q=read(),es=0,ec=0;
	for (int i=1;i<=n;i++) f[i]=i;
	for (int i=1;i<=q;i++)
	{
		int u=read()+1,v=read()+1,c=read();
		E[i]=(Enode){u,v,c};
		if (!c&&find(u)!=find(v)) f[find(u)]=find(v),es++;
		else if (c) ec++;
	}
	for (int i=1;i<=q;i++) 
		if (E[i].c&&find(E[i].u)==find(E[i].v)) { puts("No"); return 0; }
	ll t=0;
	for (int i=1;i<=n;i++) if (find(i)==i) t++;
	if (ec) puts((m-es>=t&&m-es<=t*(t-1)/2&&t>2)?"Yes":"No");
	else puts((m-es>=t-1&&m-es<=t*(t-1)/2)?"Yes":"No");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值