1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
using namespace std;

const int NOT_IN_SET = 0;

int main()
{
    int n, m, d;
    cin>>n>>m>>d;
    pair<int, int> *roads = new pair<int, int>[m];
    int *neighbours = new int[n+1];
    for(int j=0; j<n+1; j++)
        neighbours[j] = 0;
    for(int i=0; i < m; ++i){
        cin>>roads[i].first>>roads[i].second;
        neighbours[roads[i].first]++;
        neighbours[roads[i].second]++;
    }

    //First condition
    int first, second;
    bool was_removed = false;
    do{
        was_removed = false;
        for(int i=0; i < m; ++i){

            first = roads[i].first;
            if(first == NOT_IN_SET)
                continue;
            second = roads[i].second;

            if(neighbours[first] == NOT_IN_SET){
                roads[i].first = NOT_IN_SET;
                was_removed = true;
                if(neighbours[second] != NOT_IN_SET)
                    neighbours[second]--;
            }

            if(neighbours[second] == NOT_IN_SET){
                roads[i].first = NOT_IN_SET;
                was_removed = true;
                if(neighbours[first] != NOT_IN_SET)
                    neighbours[first]--;
            }

            if (neighbours[first] < d && roads[i].first != NOT_IN_SET){
                neighbours[first] = NOT_IN_SET;
                roads[i].first = NOT_IN_SET;
                was_removed = true;
                if(neighbours[second] != NOT_IN_SET)
                    neighbours[second]--;
            }

            if (neighbours[second] < d && roads[i].first != NOT_IN_SET){
                neighbours[second] = NOT_IN_SET;
                roads[i].first = NOT_IN_SET;
                was_removed = true;
                if(neighbours[first] != NOT_IN_SET)
                    neighbours[first]--;
            }
        }
    } while(was_removed);

    bool *new_neighbours = new bool[n+1];
    int counter = 0;
    for(int i=1; i<n+1; i++){
        if(neighbours[i] > 0){
            new_neighbours[i] = true;
            counter++;
        }
        else
            new_neighbours[i] = false;
        neighbours[i] = 0;
    }

    if(counter == 0){
        cout<<"NIE"<<endl;
        return 0;
    }

    pair<int, int> *new_roads = new pair<int, int>[m];
    int new_m=0;
    pair<int, int> x;
    for(int i=0; i < m; ++i){
        if(roads[i].first != NOT_IN_SET){
            x.first = roads[i].first;
            x.second = roads[i].second;
            new_roads[new_m++] = x;
        }
    }

    //Second condition
    int group_id = 1;
    do{
        was_removed = false;
        for(int i=0; i < new_m; ++i){

            first = new_roads[i].first;
            if(first == NOT_IN_SET)
                continue;
            second = new_roads[i].second;

            if(neighbours[first] == 0 && neighbours[second] == 0){
                neighbours[first] = group_id;
                neighbours[second] = group_id;
                was_removed = true;
                group_id++;
                continue;
            }

            if(neighbours[first] == neighbours[second])
                continue;

            if(neighbours[first] > 0 && neighbours[second] > 0){
                was_removed = true;
                int min_group_id = min(neighbours[first], neighbours[second]);
                neighbours[first] = min_group_id;
                neighbours[second] = min_group_id;
                continue;
            }

            was_removed = true;
            neighbours[first] = max(neighbours[first], neighbours[second]);
            neighbours[second] = max(neighbours[first], neighbours[second]);
        }
    } while(was_removed);

    int *groups = new int[group_id+1];
    for(int j=1; j<group_id+1; j++)
        groups[j] = 0;

    for(int i=1; i<n+1; i++){
        groups[neighbours[i]]++;
    }

    int max = 0;
    int max_id = 0;
    for(int i=1; i<group_id+1; i++){
        if(groups[i] > max){
            max = groups[i];
            max_id = i;
        }
    }

    vector<int> cities;
    cities.reserve(n+1);
    for(int i=1; i<n+1; i++){
        if(neighbours[i] == max_id)
            cities.push_back(i);
    }
    sort(cities.begin(), cities.end());

    cout<<cities.size()<<endl;
    for (vector<int>::iterator it=cities.begin(); it!=cities.end(); ++it)
        cout<<*it<<" ";
    cout<<endl;
    return 0;
}/**/