G. Welcome to Join the Online Meeting
思路:
挺简单的BFS思路
图论题写的比较少,算是补题吧
代码:
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
#define pb push_back
#define pii pair<int,int>
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
typedef long long ll;
using namespace std;
const int N = 200010;
int n,m,k;
int vis[N];
int busy[N];
int bzs=0;
vector<int> lj[N];
int bfs1(int cjz){
int cnt=1;
queue<int> qu;
qu.push(cjz);
vis[cjz]=1;
while(!qu.empty()){
bool flag = 0;
int f = qu.front();
qu.pop();
for(int e:lj[f]){
if(vis[e])continue;
cnt++;
vis[e]=1;
if(!busy[e]) qu.push(e);
flag=1;
}
if(flag)bzs++;
}
return cnt;
}
void bfs2(int cjz){
queue<int> qu;
qu.push(cjz);
vis[cjz]=1;
while(!qu.empty()){
int f = qu.front();
qu.pop();
vector<int> ans;
for(int e:lj[f]){
if(vis[e])continue;
vis[e]=1;
ans.push_back(e);
if(!busy[e]) qu.push(e);
}
if(ans.size()>0){
cout<<f<<" "<<ans.size()<<" ";
for(int k:ans) cout<<k<<" ";
cout<<endl;
}
}
}
void solve() {
cin>>n>>m>>k;
for(int i=1;i<=k;i++){
int x;
cin>>x;
busy[x]=1;
}
for(int i=1;i<=m;i++){
int x,y;
cin>>x>>y;
lj[x].pb(y);
lj[y].pb(x);
}
int cjz=0;
for(int i=1;i<=n;i++){
if(busy[i]==0){
cjz=i;
break;
}
}
if(cjz==0){
cout<<"No";return;
}
int cnt=bfs1(cjz);
if(cnt==n){
cout<<"Yes"<<endl<<bzs<<endl;
}else{
cout<<"No";return;
}
memset(vis,0,sizeof(vis));
bfs2(cjz);
}
signed main() {
cin.tie(0)->ios::sync_with_stdio(0);
int T = 1;
while (T--) {
solve();
}
return 0;
}