128642025-01-02 17:38:46ercseferencElágazás nélküli úton levő települések (50 pont)cpp17Accepted 50/5028ms1336 KiB
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct pont{vector<int>kim; int k=0; bool van=0;};
bool has(int a, int b) {
    return a<b;}
int main()
{
    int n,m,x,y;
    cin>>n>>m; pont a[n+1]; bool nincs=0;
    for(int i=0; i<m; i++){
        cin>>x>>y; a[x].kim.push_back(y);
        a[y].kim.push_back(x); a[x].k++; a[y].k++;}
    vector<int>zsak,l1,l2,l3;
    for(int i=1; i<=n; i++){if(a[i].k==1){zsak.push_back(i);}}
    for(int i=0; i<zsak.size(); i++){
        l1.clear(); l2.clear(); l1.push_back(zsak[i]); nincs=0;
        for(int j=1; j<=n; j++){a[j].van=0;} a[zsak[i]].van=1;
        while(nincs==0){
        if(l1.size()==0)nincs=1;
        for(int i=0; i<l1.size(); i++){
            for(int j=0; j<a[l1[i]].kim.size(); j++){
                if(a[a[l1[i]].kim[j]].van==0)
                    {l2.push_back(a[l1[i]].kim[j]); a[a[l1[i]].kim[j]].van=1;}}}
        l1.clear();
        for(int i=0; i<l2.size(); i++)
            {if(a[l2[i]].k<=2)l1.push_back(l2[i]);
            l3.push_back(l2[i]);} l2.clear();}}
    sort(l3.begin(),l3.end(),has);
    vector<int>ans;
    if(l3.size()==0)cout<<"0";
    else{
        ans.push_back(l3[0]);
        for(int i=1; i<l3.size(); i++){if(l3[i]!=l3[i-1])ans.push_back(l3[i]);}
        cout<<ans.size()<<endl;
        for(int i=0; i<ans.size(); i++){cout<<ans[i]<<" ";}}
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/01ms500 KiB
2Accepted0/028ms1336 KiB
3Accepted2/21ms320 KiB
4Accepted2/21ms320 KiB
5Accepted2/21ms320 KiB
6Accepted2/21ms320 KiB
7Accepted2/21ms320 KiB
8Accepted2/22ms320 KiB
9Accepted2/24ms504 KiB
10Accepted2/27ms624 KiB
11Accepted2/214ms820 KiB
12Accepted2/214ms872 KiB
13Accepted3/32ms512 KiB
14Accepted3/34ms320 KiB
15Accepted3/34ms596 KiB
16Accepted3/39ms720 KiB
17Accepted3/314ms824 KiB
18Accepted3/316ms996 KiB
19Accepted3/317ms1080 KiB
20Accepted3/326ms1216 KiB
21Accepted3/327ms1316 KiB
22Accepted3/328ms1152 KiB