#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fastread ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define openfile freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#define f(i, x, y) for(int i = x; i < y; i++)
#define all(X) X.begin(), X.end()
#define int long long
#define key pair<int, int>
#define ff first
#define ss second
int mod = 1e9 + 7;
const int inf = 2e18;
using namespace std;

vector<int> g[100001], rg[100001], v;
int h[100001];
stack<int> st;
bool vis[100001];

void dfs(int s)
{
    vis[s] = 1;
    for(int i : g[s])
        if(!vis[i])
            dfs(i);
    st.push(s);        
}

void rdfs(int s)
{
    vis[s] = 1;
    v.pb(s);
    for(int i : rg[s])
        if(!vis[i])
            rdfs(i);
}

main()
{
    set<key> se;
    int n, m; cin>>n>>m>>mod; 
    f(i, 1, n+1) cin>>h[i];
    f(i, 1, m+1)
    {
        int s, d; cin>>s>>d;
    //    if(se.find({s, d}) != se.end()) continue;
    //    se.insert({s, d}); 
    //    se.insert({d, s});
        if((h[s]+1) % mod == h[d])
        {
            g[s].pb(d); rg[d].pb(s);
        }
        if((h[d]+1)%mod == h[s])
        {
            g[d].pb(s); rg[s].pb(d);
        }
    }
    //f(i, 1, n+1) { for(int j : rg[i]) cout<<j<<" "; cout<<"\n"; }
    
    f(i, 1, n+1)
        if(g[i].size() == 0)
        {
            cout<<"1\n"<<i; return 0;
        }
    
    memset(vis, 0, sizeof(vis));    
    vector<int> res;
    f(i, 1, n+1) res.pb(i);
    
    f(i, 1, n+1)
        if(!vis[i])
            dfs(i);
    
    memset(vis, 0, sizeof(vis));
    while(!st.empty())
    {
        v.clear();
        int t = st.top();
//        cout<<t<<" ";
        st.pop();
        if(vis[t]) continue;
        rdfs(t);
    //    for(int i : v) cout<<i<<" "; cout<<"\n";
        if(v.size() > 1 && v.size() < res.size())  res = v;
    }
    
    cout<<res.size()<<"\n";
    for(int i : res) cout<<i<<" ";
}

Generated URL:

Output:

4
1 2 3 4 
Report Bug