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
#include <stdio.h>
#include <deque>
#include <queue>
#include <vector>
#include <algorithm>

using namespace std;

enum NodeStatus{ INACTIVE, ACTIVE, COUNTED };

struct Node{
    Node()
    : status(NodeStatus::ACTIVE)
    , counter(0){}

    NodeStatus status;
    int counter;
    deque<int> neighbours;
};

struct Edge{
    int from;
    int to;
};

int main()
{
    int N, M, D;
    
    scanf( "%d %d %d", &N, &M, &D );

    vector<Node> nodes( N );
    vector<Edge> edges( M );
    
    //read edges
    for( int i = 0; i < M; ++i ){
        scanf( "%d %d", &edges[i].from, &edges[i].to );
        --edges[i].from;
        --edges[i].to;
    }

    for( const Edge& edge : edges ){
        Node& from_node = nodes[edge.from];
        Node& to_node = nodes[edge.to];
        ++from_node.counter;
        ++to_node.counter;

        from_node.neighbours.push_back( edge.to );
        to_node.neighbours.push_back( edge.from );
    }

    //remove all nodes with counter < D
    queue<int> nodesToProcess;

    for( int i = 0; i < N; ++i ){
        if( nodes[i].counter < D )
            nodesToProcess.push( i );
    }

    while( !nodesToProcess.empty() ){
        int node_idx = nodesToProcess.front();
        nodesToProcess.pop();

        Node& node = nodes[node_idx];

        if( node.status == NodeStatus::INACTIVE )
            continue;

        node.status = NodeStatus::INACTIVE;

        for( int n_idx : node.neighbours ){
            Node& neighbour = nodes[n_idx];
            if( neighbour.status == NodeStatus::INACTIVE )
                continue;

            --neighbour.counter;
            if( neighbour.counter < D ){
                nodesToProcess.push( n_idx );
            }
        }
    }

    //find all groups of connected nodes
    deque<int> largestSet;

    for( int i = 0; i < N; ++i ){
        Node& node = nodes[i];

        if( node.status != NodeStatus::ACTIVE )
            continue;
        
        deque<int> currSet;
        queue<int> nodesToProcess;

        nodesToProcess.push( i );

        while( !nodesToProcess.empty() ){
            int node_idx = nodesToProcess.front();
            nodesToProcess.pop();
            Node& currNode = nodes[node_idx];

            if( currNode.status != NodeStatus::ACTIVE )
                continue;

            currNode.status = NodeStatus::COUNTED;
            
            for( int n_idx : currNode.neighbours ){
                Node& neighbour = nodes[n_idx];

                if( neighbour.status != NodeStatus::ACTIVE )
                    continue;
                
                nodesToProcess.push( n_idx );
            }

            currSet.push_back( node_idx );
        }

        if( currSet.size() > largestSet.size() )
            largestSet.swap(currSet);
    }

    //sort and print the largest set
    sort( largestSet.begin(), largestSet.end() );

    if( largestSet.empty() ){
        printf( "NIE\n" );
    }
    else{
        printf( "%d\n", largestSet.size() );

        for( int idx : largestSet ){
            printf( "%d ", idx + 1 );
        }
    }
}