前言

參考題解
//
// Created by niko on 2020/9/19.
//
//剩余時間:19:44
#include <bits/stdc++.h>
using namespace std;
/**
* 題意:給出n個點,m條邊的圖,接下來進行幾次查詢,每次查詢問由k個結點組成的子圖中
* 前三個度數最高的結點編號(度數降序排序),若度數相同,則按照從小到大的順序輸出
* @return
*/
vector<pair<int,int>> edge;//存盤每條邊
unordered_map<int,int> degree;//子圖中每個結點的度數
//排序規則
bool cmp(int a,int b){
if(degree[a]!=degree[b])return degree[a]>degree[b];
return a<b;
}
int main(){
//加速cin
ios::sync_with_stdio(false);
cin.tie(0);
int n,m,k,a,b;
cin>>n>>m;
while (m--){
cin>>a>>b;
edge.push_back({a,b});
}
while (cin>>k&&k!=0){
degree.clear();
vector<int> v(k);//存盤子圖中每個結點
unordered_map<int,bool> exist;//是否存在
for(int i=0;i<k;i++){
cin>>v[i];
exist[v[i]]= true;
}
//核心,遍歷每條邊,若子圖中存在該邊,則兩端的結點的度都加1
for(auto it:edge){
if(exist[it.first]&&exist[it.second]){
degree[it.first]++;
degree[it.second]++;
}
}
sort(v.begin(),v.end(),cmp);
printf("%d %d %d\n",v[0],v[1],v[2]);
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/95904.html
標籤:其他
上一篇:園區網運維經驗之ARP協議
下一篇:江蘇省的計算機二級考試c語言
