日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

AGC038D - Unique Path(建图)

發布時間:2023/12/3 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 AGC038D - Unique Path(建图) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

AGC038D - Unique Path

Solution

此題較水。
大概就是簡單路徑唯一意味著連成一棵樹,因此先給這些限制的端點放在同一個連通塊內,然后如果有多條路徑的限制的兩端點在同一個連通塊內,則無解。

然后考慮如果沒有多條路徑的限制,則還要連的邊數最少為t?1t-1t?1,最多為t(t?1)2\frac{t(t-1)}{2}2t(t?1)?ttt為連通塊個數),直接判斷即可。

如果有多條路徑,則還要連的邊數最少為ttt(一種可行方案是每個限制以以每個連通塊的根為端點連成環),最多為t(t?1)2\frac{t(t-1)}{2}2t(t?1)?,且連通塊個數至少要有兩個,否則沒法連成環。

時間復雜度O(nlgn)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 secondusing 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; }

總結

以上是生活随笔為你收集整理的AGC038D - Unique Path(建图)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。