Problem:
社員旅行
Problem Statement
あなたの会社には
n
人の社員が存在する．
m
個の社員
(a_i,b_i)
の組について，
a_i
は
b_i
の上司である．
社員
x
が社員
y
の実質的な上司であるとは，次のうち少なくとも一方が成り立つことをいう．
x
が
y
の上司である．
y
の実質的な上司である社員
z
が存在して，
x
は
z
の上司である．
あなたの会社で，自分自身が自分の実質的な上司であるような社員は存在しない．
あなたの会社では社員が全員参加する社員旅行が計画されている．全社員の要求により，旅館での部屋割りは「よい部屋割り」でなければならない．
ある部屋割りが「よい部屋割り」であるとは以下の両方が満たされることをいう．
各社員はどこかの部屋に割り振られる．
社員
x
と社員
y
が同じ部屋に割り振られているとき，
x
は
y
の実質的な上司でない．
幹事の社員は非常に優秀なので，「よい部屋割り」でかつ必要な部屋の数が最小になるように部屋割りを行った．しかし残念なことに予算が不足している．どうしても必要な部屋の数を減らさなければならないらしい．
そこで，人事部で働くあなたは上司-部下の関係を一つだけ解消することによって，「よい部屋割り」を得るために必要な部屋の数を減らすことにした．
さて，どの関係を解消すればよいのだろう？
Input
入力は以下の形式に従う．与えられる数は全て整数である．
n
m
a_1
b_1
...
a_m
b_m
Constraints
2≦n≦10^5
1≦m≦2 \times 10^5
1≦a_i<b_i≦n
i \neq j
ならば
(a_i, b_i) \neq (a_j, b_j)
Output
次を満たすような
i
を昇順に1行ずつ出力せよ．
「
a_i
が
b_i
の上司である」という関係を解消したとき，「よい部屋割り」を得るために必要な部屋の数を減らすことができる．
そのような
i
が存在しない場合は-1を1行に出力せよ．
Sample Input 1
5 4
1 2
2 3
3 4
3 5
Output for the Sample Input 1
1
2

Submitted solutions:

code_snippet: // AOJ #2500 Company Trip
// // 2025.4.30

#include <bits/stdc++.h>
using namespace std;

#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)

int Cin() {
	int n = 0; int c = gc();
	if (c == '-') {	c = gc();
		do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
		return -n;
	}
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

void Cout(int n) {
	char b[30];
	if (!n) pc('0');
	else {
		if (n < 0) pc('-'), n = -n;
		int i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
	pc('\n');
}

int main() {
    int n = Cin(), m = Cin();

    vector<vector<int>> g(n+1);
    vector<int> in(n+1, 0), d(n+1, 1), r(n+1, 1);
    vector<int> a(m+1), b(m+1);

    for (int i = 1; i <= m; i++) {
        a[i] = Cin(), b[i] = Cin();
        g[a[i]].push_back(b[i]);
        in[b[i]]++;
    }

    queue<int> q;
    for (int i = 1; i <= n; i++) if (in[i] == 0) q.push(i);

    vector<int> topo;
    topo.reserve(n);
    while (!q.empty()) {
        int u = q.front(); q.pop();
        topo.push_back(u);
        for (int v : g[u]) if (--in[v] == 0) q.push(v);
    }

    for (int u : topo) for (int v : g[u]) d[v] = max(d[v], d[u] + 1);

    int H = *max_element(d.begin()+1, d.end());

    for (int i = n-1; i >= 0; i--) {
        int u = topo[i];
        for (int v : g[u]) r[u] = max(r[u], r[v] + 1);
    }

    vector<int> cnt(H+2, 0);
    for (int i = 1; i <= m; i++) {
        int u = a[i], v = b[i];
        if (d[u] + r[v] == H) cnt[d[u]]++;
    }

    vector<int> ans;
    for (int i = 1; i <= m; i++) {
        int u = a[i], v = b[i];
        if (d[u] + r[v] == H && cnt[d[u]] == 1) ans.push_back(i);
    }

    if (ans.empty()) Cout(-1);
    else for (int i : ans) Cout(i);
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 11680
final_rank: 1

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



#define SIZE 100005

struct  Edge{
	int from,to;
};

int V,E;
int max_height = 0;
int height[SIZE],in_num[SIZE],out_num[SIZE],DEPTH[SIZE];
int num_height[SIZE];
Edge edge[2*SIZE];
vector<int> G[SIZE],rev_G[SIZE];



int main(){

	scanf("%d %d",&V,&E);

	for(int i = 0; i < V; i++){

		in_num[i] = 0;
		out_num[i] = 0;
		num_height[i] = 0;
		DEPTH[i] = -BIG_NUM;
	}

	for(int i = 0; i < E; i++){

		scanf("%d %d",&edge[i].from,&edge[i].to);
		edge[i].from--;
		edge[i].to--;
		in_num[edge[i].to]++;
		out_num[edge[i].from]++;
		G[edge[i].from].push_back(edge[i].to);
		rev_G[edge[i].to].push_back(edge[i].from);
	}

	queue<int> Q;

	for(int i = 0; i < V; i++){
		if(in_num[i] == 0){

			DEPTH[i] = 0;
			Q.push(i);
		}
	}

	//トポロジカル順に最大深さを求める
	while(!Q.empty()){

		int node_id = Q.front();
		Q.pop();

		for(int i = 0; i < G[node_id].size(); i++){

			int child = G[node_id][i];
			DEPTH[child] = max(DEPTH[child],DEPTH[node_id]+1);
			in_num[child]--;
			if(in_num[child] == 0){

				Q.push(child);
			}
		}
	}

	for(int i = 0; i < V; i++){
		if(out_num[i] == 0){

			height[i] = 0;
			Q.push(i);
		}
	}

	//トポロジカル順に最大高さを求める
	while(!Q.empty()){

		int node_id = Q.front();
		Q.pop();

		for(int i = 0; i < rev_G[node_id].size(); i++){

			int parent = rev_G[node_id][i];
			height[parent] = max(height[parent],height[node_id]+1);
			max_height = max(max_height,height[parent]);
			out_num[parent]--;
			if(out_num[parent] == 0){

				Q.push(parent);
			}
		}
	}

	for(int i = 0; i < E; i++){

		if(DEPTH[edge[i].from]+height[edge[i].to]+1 != max_height)continue; //最長パス上にないエッジは無視

		num_height[height[edge[i].to]]++;
	}
	bool FLG = false;

	for(int i = 0; i < E; i++){

		if(DEPTH[edge[i].from]+height[edge[i].to]+1 != max_height)continue; //最長パス上にないエッジは無視

		if(num_height[height[edge[i].to]] == 1){

			printf("%d\n",i+1);
			FLG = true;
		}
	}

	if(!FLG){

		printf("-1\n");
	}

	return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 16900
final_rank: 5

code_snippet: #include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
#include <utility>
using namespace std;
 
typedef pair<int,int> pii;
 
int main(){
    int n, m;
    scanf("%d%d", &n, &m);
     
    vector<pii> rel(m + n + 1);
    vector<vector<int> > G(n + 1), from(n + 1);
    vector<int> cnt(n + 1);
    for(int i = 1; i <= m; ++i){
        scanf("%d%d", &rel[i].first, &rel[i].second);
        G[rel[i].first].push_back(i);
        ++cnt[rel[i].second];
    }
    for(int i = 1; i <= n; ++i){
        rel[i + m] = pii(i, 0);
        G[i].push_back(i + m);
        ++cnt[0];
    }
 
    vector<int> d(n + 1, -1);
    queue<int> q;
    for(int i = 0; i <= n; ++i){
        if(cnt[i] == 0){
            d[i] = 0;
            q.push(i);
        }
    }
    q.push(-1);
    int t = 0;
    while(q.size() > 1){
        int a = q.front();
        q.pop();
        if(a == -1){
            q.push(-1);
            ++t;
        }
        else if(d[a] == t){
            for(int i = 0; i < G[a].size(); ++i){
                int u = rel[G[a][i]].second;
                if(d[u] <= t){
                    d[u] = t + 1;
                    from[u].clear();
                }
                if(d[u] == t + 1){
                    from[u].push_back(G[a][i]);
                }
                 
                if(--cnt[u] == 0){
                    q.push(u);
                }
            }
        }
    }
     
    vector<int> ans;
    q.push(0);
    vector<char> vis(n + 1);
    vis[0] = 1;
    while(q.size() > 1){
        int a = q.front();
        q.pop();
        if(a == -1){
            q.push(-1);
        }
        else{
            if(a != 0 && q.size() == 1 && from[a].size() == 1){
                ans.push_back(from[a][0]);
            }
             
            for(int i = 0; i < from[a].size(); ++i){
                int u = rel[from[a][i]].first;
                if(!vis[u]){
                    vis[u] = 1;
                    q.push(u);
                }
            }
        }
    }
     
    sort(ans.begin(), ans.end());
    if(ans.empty()){ ans.push_back(-1); }
    for(int i = 0; i < ans.size(); ++i){
        printf("%d\n", ans[i]);
    }
}
accuracy: 1
time_ms: 70
memory_kb: 16392
final_rank: 4

code_snippet: #include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
 
const int MAX_N = int(1e5);
const int MAX_M = int(2e5);
 
template<typename numType>
inline bool updateMax(numType& old, const numType& test) {
    if (old < test) {
        old = test;
        return true;
    }
    return false;
}
 
struct Edge {
    int from, to;
 
    Edge(){}
    Edge(int from_, int to_):
        from(from_), to(to_)
    {}
};
 
int N, M;
vector<int> ids[MAX_N];
vector<Edge> graph[MAX_N];
Edge es[MAX_M];
 
int lengthFromS[MAX_N], lengthFromT[MAX_N];
 
void init() {
    scanf("%d%d", &N, &M);
    for (int i = 0; i < M; ++i) {
        int a, b;
        scanf("%d%d", &a, &b);
        --a; --b;
        es[i] = Edge(a, b);
        graph[a].push_back(Edge(a, b));
        graph[b].push_back(Edge(b, a));
    }
}
 
void solve() {
    int height = 0;
    for (int v = 0; v < N; ++v) {
        updateMax(height, lengthFromS[v]);
        for (int i = 0; i < int(graph[v].size()); ++i) {
            const Edge &e = graph[v][i];
            const int u = e.to;
            if (v < u) {
                updateMax(lengthFromS[u], lengthFromS[v] + 1);
            }
        }
    }
 
    for (int v = N - 1 ; v >= 0; --v) {
        for (int i = 0; i < int(graph[v].size()); ++i) {
            const Edge &e = graph[v][i];
            const int u = e.to;
            if (v > u) {
                updateMax(lengthFromT[u], lengthFromT[v] + 1);
            }
        }
    }
 
    for (int i = 0; i < M; ++i) {
        if (lengthFromS[es[i].from] + 1 + lengthFromT[es[i].to] == height) {
            ids[lengthFromS[es[i].from]].push_back(i);
        }
    }
 
    vector<int> ans;
    for (int i = 0; i < N; ++i) {
        if (int(ids[i].size()) == 1) {
            ans.push_back(ids[i][0]);
        }
    }
    sort(ans.begin(), ans.end());
    if (ans.empty()) {
        puts("-1");
    }
    else {
        for (int i = 0; i < int(ans.size()); ++i) {
            printf("%d\n", ans[i]+1);
        }
    }
}
 
int main() {
    init();
    solve();
    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 15884
final_rank: 3

code_snippet: #include <iostream>
#include <vector>
#include <queue>
#include <bitset>
#include <map>
#include <algorithm>

using namespace std;

bool solve() {
    int N, M;
    if(!(cin >> N >> M)) return false;
    if(!N && !M) return false;

    vector<vector<int>> graph(N);
    vector<vector<int>> rev_graph(N);
    map<pair<int,int>,int> edge_dict;
    vector<int> degree(N, 0);
    for(int i = 0; i < M; ++i) {
        int a, b;
        cin >> a >> b;
        --a; --b;
        graph[b].push_back(a);
        rev_graph[a].push_back(b);
        ++degree[a];
        edge_dict[make_pair(b, a)] = i;
    }

    vector<int> dp(N, 0);
    vector<int> prev(N, -1);
    vector<bool> longest_path(N, false);
    for(int i = N-1; i >= 0; --i) {
        if(degree[i] > 0) continue;
        queue<int> q;
        q.push(i);
        while(!q.empty()) {
            const int cur = q.front();
            q.pop();
            for(int neigh : graph[cur]) {
                if(dp[neigh] < dp[cur]+1) {
                    dp[neigh] = dp[cur] + 1;
                    prev[neigh] = cur;
                }
                if(--degree[neigh] == 0) {
                    q.push(neigh);
                }
            }
        }
    }
    const int longest = *max_element(dp.begin(), dp.end());
    bitset<200010> res;
    res.set();
    for(int i = 0; i < N; ++i) {
        bitset<200010> tmp;
        if(dp[i] == longest) {
            vector<int> q[2];
            q[0].push_back(i);
            while(q[0].size() > 0) {
                q[1].clear();
                for(int cur : q[0]) {
                    for(int neigh : rev_graph[cur]) {
                        if(dp[neigh] == dp[cur] - 1) {
                            q[1].push_back(neigh);
                        }
                    }
                }
                sort(q[1].begin(), q[1].end());
                q[1].erase(unique(q[1].begin(), q[1].end()), q[1].end());
                if(q[0].size() == 1 && q[1].size() == 1) {
                    tmp[edge_dict[make_pair(q[1][0], q[0][0])]] = true;
                }
                q[0].swap(q[1]);
            }
            res &= tmp;
        }
    }
    if(res.none()) {
        cout << -1 << endl;
    } else {
        for(int i = 0; i < M; ++i) {
            if(res[i]) cout << i+1 << endl;
        }
    }
    return true;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);

    while(solve()) ;
    return 0;
}
accuracy: 1
time_ms: 270
memory_kb: 24228
final_rank: 15

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
typedef pair<int, int> P;

vector<P> down[105000], up[105000];
vector<int> ans[105000], res, topo;
int dp[105000];
bool come[105000];
int longest, n, m, a, b;
void toposort(int x){
    if(come[x])return;
    come[x] = true;
    for(int i = 0;i < up[x].size();i++){
	toposort(up[x][i].first);
    }
    topo.push_back(x);
}

void back(int x){
    if(come[x])return;
    come[x] = true;
    for(int i = 0;i < down[x].size();i++){
	int to = down[x][i].first;
	int tmp = down[x][i].second;
	if(dp[to] != dp[x] - 1)continue;
	ans[dp[x]].push_back(tmp);
	back(to);
    }
}

int main(){
    cin >> n >> m;
    for(int i = 0;i < m;i++){
	cin >> a >> b;
	up[a].push_back(P(b, i + 1));
	down[b].push_back(P(a, i + 1));
    }
    for(int i = 1;i <= n;i++)
	toposort(i);

    for(int i = n - 1;i >= 0;i--){
	int from = topo[i];
	longest = max(longest, dp[from]);
	for(int j = 0;j < up[from].size();j++){
	    int to = up[from][j].first;
	    dp[to] = max(dp[to], dp[from] + 1);	    
	}
    }
    fill(come, come + 105000, false);
    for(int i = 1;i <= n;i++)
	if(dp[i] == longest)back(i);

    for(int i = 1;i <= n;i++){
	if(ans[i].size() == 1){
	    res.push_back(ans[i][0]);
	}
    }

    sort(res.begin(), res.end());
    for(int i = 0;i < res.size();i++)
	cout << res[i] << endl;
    if(res.size() == 0)
	cout << -1 << endl;

    return 0;
}
accuracy: 1
time_ms: 230
memory_kb: 25652
final_rank: 14

code_snippet: #include <map>
#include <algorithm>
#include <iostream>
#define REP(i,n) for(int i=0; i<(int)(n); i++)

#include <queue>
#include <set>
#include <cstdio>
inline int getInt(){ int s; scanf("%d", &s); return s; }

using namespace std;

bool one(double x){
  return x + 1e-9 > 1.0;
}

int main(){
  const int n = getInt();
  const int m = getInt();

  vector<vector<int> > v(n);
  vector<vector<int> > r(n);
  vector<int> intmp;
  vector<int> in(n);
  vector<int> out(n);
  vector<pair<int, int> > e(m);
  map<int, map<int, int> > memo;

  REP(i,m){
    const int a = e[i].first  = getInt() - 1;
    const int b = e[i].second = getInt() - 1;

    v[a].push_back(b);
    in[b]++;

    r[b].push_back(a);
    out[a]++;

    memo[a][b] = memo[b][a] = i;
  }
  intmp = in;

  vector<int> dist(n);
  queue<int> q;

  REP(i,n) if(in[i] == 0){
    q.push(i);
  }

  while(q.size()){
    const int pos = q.front(); q.pop();

    for(const int next : v[pos]){
      dist[next] = max(dist[next], dist[pos] + 1);
      --in[next];
      if(in[next] == 0) q.push(next);
    }
  }

  const int mx = *max_element(dist.begin(), dist.end());
  vector<int> chain(m);
  vector<int> valid(n);

  REP(i,n) if(dist[i] == mx){
    valid[i] = 1;
  }

  REP(i,n) if(out[i] == 0){
    q.push(i);
  }

  while(q.size()){
    const int pos = q.front(); q.pop();

    for(const int next : r[pos]){
      if(dist[pos] == dist[next] + 1){
        if(valid[pos]){
          valid[next] = 1;
          chain[memo[pos][next]] = 1;
        }
      }

      --out[next];
      if(out[next] == 0) q.push(next);
    }
  }

  vector<double> wp(n);
  vector<double> w(m);
  in = intmp;

  int cnt = 0;
  REP(i,n) if(in[i] == 0 && valid[i]){
    cnt++;
  }

  REP(i,n) if(in[i] == 0 && valid[i]){
    wp[i] = 1.0 / cnt;
  }

  REP(i,n) if(in[i] == 0){
    q.push(i);
  }

  while(q.size()){
    const int pos = q.front(); q.pop();

    int cnt = 0;
    for(const int next : v[pos])
      if(chain[memo[pos][next]]) cnt++;

    for(const int next : v[pos]){
      if(chain[memo[pos][next]]){
        wp[next] += wp[pos] / cnt;
        w[memo[pos][next]] += wp[pos] / cnt;
      }
      --in[next];
      if(in[next] == 0) q.push(next);
    }
  }

  bool b = false;
  REP(i,m){
    if(one(w[i])){
      printf("%d\n", i + 1);
      b = true;
    }
  }

  if(!b) puts("-1");

  return 0;
}
accuracy: 1
time_ms: 430
memory_kb: 44772
final_rank: 17

code_snippet: #include <map>
#include <algorithm>
#include <iostream>
#define REP(i,n) for(int i=0; i<(int)(n); i++)

#include <queue>
#include <set>
#include <cstdio>
inline int getInt(){ int s; scanf("%d", &s); return s; }

using namespace std;

bool one(double x){
  return x + 1e-9 > 1.0;
}

int main(){
  const int n = getInt();
  const int m = getInt();

  vector<vector<int> > v(n);
  vector<vector<int> > r(n);
  vector<int> intmp;
  vector<int> in(n);
  vector<int> out(n);
  vector<pair<int, int> > e(m);
  map<int, map<int, int> > memo;

  REP(i,m){
    const int a = e[i].first  = getInt() - 1;
    const int b = e[i].second = getInt() - 1;

    v[a].push_back(b);
    in[b]++;

    r[b].push_back(a);
    out[a]++;

    memo[a][b] = memo[b][a] = i;
  }
  intmp = in;

  vector<int> dist(n);
  queue<int> q;

  REP(i,n) if(in[i] == 0){
    q.push(i);
  }

  while(q.size()){
    const int pos = q.front(); q.pop();

    for(const int next : v[pos]){
      dist[next] = max(dist[next], dist[pos] + 1);
      --in[next];
      if(in[next] == 0) q.push(next);
    }
  }

  const int mx = *max_element(dist.begin(), dist.end());
  vector<int> chain(m);
  vector<int> valid(m);

  REP(i,n) if(dist[i] == mx){
    valid[i] = 1;
  }

  REP(i,n) if(out[i] == 0){
    q.push(i);
  }

  while(q.size()){
    const int pos = q.front(); q.pop();

    for(const int next : r[pos]){
      if(dist[pos] == dist[next] + 1){
        if(valid[pos]){
          valid[next] = 1;
          chain[memo[pos][next]] = 1;
        }
      }

      --out[next];
      if(out[next] == 0) q.push(next);
    }
  }

  vector<double> wp(n);
  vector<double> w(m);
  in = intmp;

  int cnt = 0;
  REP(i,n) if(in[i] == 0 && valid[i]){
    cnt++;
  }

  REP(i,n) if(in[i] == 0 && valid[i]){
    wp[i] = 1.0 / cnt;
  }

  REP(i,n) if(in[i] == 0){
    q.push(i);
  }

  while(q.size()){
    const int pos = q.front(); q.pop();

    int cnt = 0;
    for(const int next : v[pos])
      if(chain[memo[pos][next]]) cnt++;

    for(const int next : v[pos]){
      if(chain[memo[pos][next]]){
        wp[next] += wp[pos] / cnt;
        w[memo[pos][next]] += wp[pos] / cnt;
      }
      --in[next];
      if(in[next] == 0) q.push(next);
    }
  }

  bool b = false;
  REP(i,m){
    if(one(w[i])){
      printf("%d\n", i + 1);
      b = true;
    }
  }

  if(!b) puts("-1");

  return 0;
}
accuracy: 0.47368421052631576
time_ms: 400
memory_kb: 41064
final_rank: 18

code_snippet: #include <map>
#include <algorithm>
#include <iostream>
#define REP(i,n) for(int i=0; i<(int)(n); i++)

#include <queue>
#include <set>
#include <cstdio>
inline int getInt(){ int s; scanf("%d", &s); return s; }

using namespace std;

bool one(double x){
  return x + 1e-9 > 1.0;
}

int main(){
  const int n = getInt();
  const int m = getInt();

  vector<vector<int> > v(n);
  vector<vector<int> > r(n);
  vector<int> intmp;
  vector<int> in(n);
  vector<int> out(n);
  vector<pair<int, int> > e(m);
  map<int, map<int, int> > memo;

  REP(i,m){
    const int a = e[i].first  = getInt() - 1;
    const int b = e[i].second = getInt() - 1;

    v[a].push_back(b);
    in[b]++;

    r[b].push_back(a);
    out[a]++;

    memo[a][b] = memo[b][a] = i;
  }
  intmp = in;

  vector<int> dist(n);
  queue<int> q;

  REP(i,n) if(in[i] == 0){
    q.push(i);
  }

  while(q.size()){
    const int pos = q.front(); q.pop();

    for(const int next : v[pos]){
      dist[next] = max(dist[next], dist[pos] + 1);
      --in[next];
      if(in[next] == 0) q.push(next);
    }
  }

  const int mx = *max_element(dist.begin(), dist.end());
  vector<int> chain(m);
  vector<int> valid(m);

  REP(i,n) if(dist[i] == mx){
    valid[i] = 1;
  }

  REP(i,n) if(out[i] == 0){
    q.push(i);
  }

  while(q.size()){
    const int pos = q.front(); q.pop();

    for(const int next : r[pos]){
      if(dist[pos] == dist[next] + 1){
        if(valid[pos]){
          valid[next] = 1;
          chain[memo[pos][next]] = 1;
        }
      }

      --out[next];
      if(out[next] == 0) q.push(next);
    }
  }

  vector<double> wp(n);
  vector<double> w(m);
  in = intmp;

  int cnt = 0;
  REP(i,n) if(in[i] == 0 && valid[i]){
    cnt++;
  }

  REP(i,n) if(in[i] == 0 && valid[i]){
    wp[i] = 1.0 / cnt;
  }

  REP(i,n) if(in[i] == 0){
    q.push(i);
  }

  while(q.size()){
    const int pos = q.front(); q.pop();

    int cnt = 0;
    for(const int next : v[pos])
      if(chain[memo[pos][next]]) cnt++;

    for(const int next : v[pos]){
      if(chain[memo[pos][next]]){
        wp[next] += wp[pos] / cnt;
        w[memo[pos][next]] += wp[pos] / cnt;
      }
      --in[next];
      if(in[next] == 0) q.push(next);
    }
  }

  REP(i,m){
    if(one(w[i])){
      printf("%d\n", i + 1);
    }
  }

  return 0;
}
accuracy: 0.05263157894736842
time_ms: 10
memory_kb: 3480
final_rank: 19

code_snippet: #include <map>
#include <algorithm>
#include <iostream>
#define REP(i,n) for(int i=0; i<(int)(n); i++)

#include <queue>
#include <set>
#include <cstdio>
inline int getInt(){ int s; scanf("%d", &s); return s; }

using namespace std;

bool one(double x){
  return x + 1e-9 > 1.0;
}

int main(){
  const int n = getInt();
  const int m = getInt();

  vector<vector<int> > v(n);
  vector<vector<int> > r(n);
  vector<int> intmp;
  vector<int> in(n);
  vector<int> out(n);
  vector<pair<int, int> > e(m);
  map<int, map<int, int> > memo;

  REP(i,m){
    const int a = e[i].first  = getInt() - 1;
    const int b = e[i].second = getInt() - 1;

    v[a].push_back(b);
    in[b]++;

    r[b].push_back(a);
    out[a]++;

    memo[a][b] = memo[b][a] = i;
  }
  intmp = in;

  vector<int> dist(n);
  queue<int> q;

  REP(i,n) if(in[i] == 0){
    q.push(i);
  }

  while(q.size()){
    const int pos = q.front(); q.pop();

    for(const int next : v[pos]){
      dist[next] = max(dist[next], dist[pos] + 1);
      --in[next];
      if(in[next] == 0) q.push(next);
    }
  }

  const int mx = *max_element(dist.begin(), dist.end());
  vector<int> chain(m);
  vector<int> valid(m);

  REP(i,n) if(dist[i] == mx){
    valid[i] = 1;
  }

  REP(i,n) if(out[i] == 0){
    q.push(i);
  }

  while(q.size()){
    const int pos = q.front(); q.pop();

    for(const int next : r[pos]){
      if(dist[pos] == dist[next] + 1){
        if(valid[pos]){
          valid[next] = 1;
          chain[memo[pos][next]] = 1;
        }
      }

      --out[next];
      if(out[next] == 0) q.push(next);
    }
  }

  vector<double> wp(n);
  vector<double> w(m);
  in = intmp;

  REP(i,n) if(in[i] == 0 && valid[i]){
    wp[i] = 1.0;
  }

  REP(i,n) if(in[i] == 0){
    q.push(i);
  }

  while(q.size()){
    const int pos = q.front(); q.pop();

    int cnt = 0;
    for(const int next : v[pos])
      if(chain[memo[pos][next]]) cnt++;

    for(const int next : v[pos]){
      if(chain[memo[pos][next]]){
        wp[next] += wp[pos] / cnt;
        w[memo[pos][next]] += wp[pos] / cnt;
      }
      --in[next];
      if(in[next] == 0) q.push(next);
    }
  }

  REP(i,m){
    if(one(w[i])){
      printf("%d\n", i + 1);
    }
  }

  return 0;
}
accuracy: 0.05263157894736842
time_ms: 20
memory_kb: 3508
final_rank: 20

code_snippet: #include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef pair<int,int> pint;

#define DE 1
#define FI first
#define SE second
#define PB push_back
#define MP make_pair
#define ALL(s) (s).begin(),(s).end()
#define REP(i,n) for (int i = 0; i < (int)(n); ++i)
#define EACH(i,s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl

template<class T1, class T2> ostream& operator<<(ostream &s, pair<T1,T2> P){return s<<'<'<<P.first<<", "<<P.second<<'>';}
template<class T> ostream& operator<<(ostream &s, vector<T> P) {s<<"{ ";for(int i=0;i<P.size();++i){if(i>0)s<<", ";s<<P[i];}return s<<" }"<<endl;}
template<class T1, class T2> ostream& operator<<(ostream &s, map<T1,T2> P) {s<<"{ ";for(__typeof__(P.begin()) it=P.begin();it!=P.end();++it){if(it!=P.begin())s<<", ";s<<'<'<<it->first<<"->"<<it->second<<'>';}return s<<" }"<<endl;}



const int MAX_V = 100100;
const int MAX_E = 200200;

struct Graph {
    int V,E;
    vector<pint> list[MAX_V];
    vector<int> back[MAX_V];
    
    Graph(int n = 0) : V(n), E(0) {}
    void init(int n = 0) {V = n; E = 0; for (int i = 0; i < MAX_V; ++i) {list[i].clear(); back[i].clear();} }
    inline vector<pint>& operator [] (int i) {return list[i];}
    
    void direct(int f, int t, int num) {
        ++E;
        list[f].push_back(MP(t,num));
        back[t].push_back(f);
    }
} G; 

ostream& operator << (ostream& s, const Graph& G) {
    for (int i = 0; i < G.V; ++i) {s << i << " : " << G.list[i];}
    return s;
}

int n, m;
int a, b;
int s, t;

int dist1[MAX_V];
int dist2[MAX_V];

int rec1(int v) {
    if (dist1[v] != -1) return dist1[v];
    if (v == t) return 0;
    int res = -1;
    for (int i = 0; i < G[v].size(); ++i) {
        int to = G[v][i].FI;
        int dep = rec1(to) + 1;
        res = max(res, dep);
    }
    //cout << "1 " << v << " : " << res << endl;
    return dist1[v] = res;
}

int rec2(int v) {
    if (dist2[v] != -1) return dist2[v];
    if (v == s) return 0;
    int res = -1;
    for (int i = 0; i < G.back[v].size(); ++i) {
        int to = G.back[v][i];
        int dep = rec2(to) + 1;
        res = max(res, dep);
    }
    //cout << "2 " << v << " : " << res << endl;
    return dist2[v] = res;
}

vector<int> num[MAX_V];

int main() {
    //freopen( "/Users/macuser/Documents/Programming/Contest/input.in", "r", stdin );
    
    while (cin >> n >> m) {
        G.init(n+2);
        s = 0, t = n+1;
        int id = 1;
        for (int i = 0; i < m; ++i) {
            scanf("%d %d", &a, &b);
            G.direct(a, b, id); ++id;
        }
        for (int i = 1; i <= n; ++i) {G.direct(s, i, 0);}
        for (int i = 1; i <= n; ++i) {G.direct(i, t, 0);}
        
        //COUT(G);
        
        memset(dist1, -1, sizeof(dist1));
        memset(dist2, -1, sizeof(dist2));
        int len = rec1(s);
        rec2(t);
        
        for (int i = 0; i < MAX_V; ++i) num[i].clear();
        for (int i = 1; i <= n; ++i) {
            for (int j = 0; j < G[i].size(); ++j) {
                int from = i, to = G[i][j].FI, id = G[i][j].SE;
                if (dist1[from] == dist1[to] + 1 && dist2[to] == dist2[from] + 1 
                    && dist1[to] + dist2[to] == len) {
                    num[dist1[to]].PB(id);
                }
            }
        }
        
        vint res;
        for (int i = 0; i <= n; ++i) {
            //cout << i << " : " << num[i];
            if (num[i].size() == 1) res.PB(num[i][0]);
        }
        sort(ALL(res));
        
        for (int i = 0; i < res.size(); ++i) cout << res[i] << endl;
        if (res.size() == 0) cout << -1 << endl;
    }
    
    return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 25164
final_rank: 11

code_snippet: #include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <climits>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define chmax(a,b) (a<(b)?(a=b,1):0)
#define chmin(a,b) (a>(b)?(a=b,1):0)
#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1<<29;
const double PI = acos(-1);
const double EPS = 1e-8;

int dist1[100000];
int dist2[100000];
vector<int> g[100000];
vector<int> v[100000];
int a[200000];
int b[200000];

int main() {
  int n,m;
  while(cin >> n >> m) {
    REP(i,n) {
      v[i].clear();
      g[i].clear();
    }
    REP(i,m) {
      scanf("%d%d",a+i,b+i);
      a[i]--;b[i]--;
      g[a[i]].push_back(b[i]);
    }
    memset(dist1,0,sizeof(dist1));
    memset(dist2,0,sizeof(dist2));
    int maxLen = 0;
    REP(i,n) {
      FOR(it, g[i]) {
        chmax(dist1[*it],dist1[i] + 1);
      }
      chmax(maxLen, dist1[i]);
    }
    for (int i=n-1; i>=0; --i) {
      FOR(it, g[i]) {
        chmax(dist2[i],dist2[*it] + 1);
      }
    }
    REP(i,m) {
      if (maxLen == dist1[a[i]] + 1 + dist2[b[i]]) {
        v[dist1[a[i]]].push_back(i);
      }
    }
    vector<int> ans;
    REP(i,n) if (v[i].size() == 1) ans.push_back(v[i][0]);
    if(ans.size() == 0) cout << -1 << endl;
    else {
      sort(ALL(ans));
      FOR(it, ans) cout << *it + 1 << endl;
    }
  }
}
accuracy: 1
time_ms: 170
memory_kb: 14232
final_rank: 8

code_snippet: #include <algorithm>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;

template<class T> inline void chmax(T& a, const T& b) { if(b > a) a = b; }

void calc_dp(vector<int>& dp, vector<int>& degree, const vector<vector<int> >& edge) {
	const int n = dp.size();
	queue<int> que;
	for(int i = 0; i < n; ++i)
		if(!degree[i])
			que.push(i);

	while(!que.empty()) {
		const int v = que.front();
		que.pop();

		for(int i = 0; i < edge[v].size(); ++i) {
			chmax(dp[edge[v][i]], dp[v] + 1);
			if(!(--degree[edge[v][i]]))
				que.push(edge[v][i]);
		}
	}
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n, m;
	cin >> n >> m;

	vector<int> in(n, 0), out(n, 0);
	vector<vector<int> > es(n), rev(n);
	vector<pair<int, int> > assoc;
	assoc.reserve(m);
	for(int i = 0; i < m; ++i) {
		int a, b;
		cin >> a >> b;
		--a;
		--b;

		es[a].push_back(b);
		rev[b].push_back(a);
		++out[a];
		++in[b];
		assoc.push_back(make_pair(a, b));
	}

	vector<int> dp_source(n, 0), dp_sink(n, 0);
	calc_dp(dp_source, in, es);
	calc_dp(dp_sink, out, rev);


	int length = *max_element(dp_source.begin(), dp_source.end());
	vector<pair<int, int> > candidate;
	for(int i = 0; i < m; ++i)
		if(dp_source[assoc[i].first] + 1 + dp_sink[assoc[i].second] == length)
			candidate.push_back(make_pair(dp_source[assoc[i].first], i + 1));

	vector<int> cnt(n, 0);
	for(int i = 0; i < candidate.size(); ++i)
		++cnt[candidate[i].first];

	bool exist = false;
	for(int i = 0; i < candidate.size(); ++i) {
		if(cnt[candidate[i].first] == 1) {
			cout << candidate[i].second << endl;
			exist = true;
		}
	}

	if(!exist)
		cout << -1 << endl;

	return EXIT_SUCCESS;
}
accuracy: 1
time_ms: 170
memory_kb: 15624
final_rank: 9

code_snippet: #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>

#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it,X) for(__typeof((X).begin()) it=(X).begin();it!=(X).end();it++)
#define ite iterator
#define mp make_pair
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define pb push_back
#define sec second
#define sz(x) ((int)x.size())

using namespace std;

struct timer{
	time_t start;
	timer(){start=clock();}
	~timer(){cerr<<1.*(clock()-start)/CLOCKS_PER_SEC<<" secs"<<endl;}
};

typedef istringstream iss;
typedef long long ll;
typedef pair<int,int> pi;
typedef stringstream sst;
typedef vector<int> vi;

int n,m;
vector<pi> G[100010];
int dp[100010];
int used[100010];

int main(){
	cin.tie(0);
	ios_base::sync_with_stdio(0);
	
	cin>>n>>m;
	int a,b;
	rep(i,m){
		cin>>a>>b;
		a--,b--;
		G[b].pb(mp(a,i+1));
	}
	int M=0;
	rep(i,n){
		rep(j,sz(G[i])){
			dp[i]=max(dp[i],dp[G[i][j].fir]+1);
		}
		M=max(M,dp[i]);
	}
	vi v,w,ans;
	rep(i,n)if(dp[i]==M)v.pb(i);
	while(dp[v[0]]){
		//rep(i,sz(v))cout<<v[i]<<" ";cout<<endl;
		w.clear();
		rep(i,sz(v)){
			rep(j,sz(G[v[i]])){
				if(!used[G[v[i]][j].fir] && dp[G[v[i]][j].fir] == dp[v[i]]-1){
					w.pb(G[v[i]][j].fir);
					used[G[v[i]][j].fir]=1;
				}
			}
		}
		if(sz(v)==1 && sz(w)==1){
			rep(i,sz(G[v[0]])){
				if(dp[G[v[0]][i].fir] == dp[v[0]]-1){
					ans.pb(G[v[0]][i].sec);
				}
			}
		}
		v=w;
	}
	sort(ans.begin(),ans.end());
	if(sz(ans)==0)cout<<-1<<endl;
	else rep(i,sz(ans))cout<<ans[i]<<endl;
}
accuracy: 1
time_ms: 150
memory_kb: 7848
final_rank: 2

code_snippet: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
#include <fstream>
#include <bitset>
   
using namespace std;
   
 
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)
#define rforeach(it, c) for (__typeof__((c).rbegin()) it=(c).rbegin(); it != (c).rend(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CL(arr, val) memset(arr, val, sizeof(arr))
#define COPY(dest, src) memcpy(dest, src, sizeof(dest))
#define ten(n) ((long long)(1e##n))
#define bin(n) (1ull << (n))
#define erep(i, n) for (int i = 0; i <= (int)(n); ++i)
#define revrep(i, n) for (int i = (n); i >= 0; --i)
#define pb push_back

template <class T> void chmax(T& a, const T& b) { a = max(a, b); }
template <class T> void chmin(T& a, const T& b) { a = min(a, b); }

template <class T> void uniq(T& c) { sort(c.begin(), c.end()); c.erase(unique(c.begin(), c.end()), c.end()); }
 
template <class T> string to_s(const T& a) { ostringstream os; os << a; return os.str(); }
template <class T> T to_T(const string& s) { istringstream is(s); T res; is >> res; return res; }
  
template <class T, class U> ostream& operator<<(ostream& os, pair<T, U>& p) { os << "( " << p.first << ", " << p.second << " )"; return os; }
  
template <class T> void print(T a, int n, const string& deli = " ", int br = 1) { for (int i = 0; i < n; ++i) { cout << a[i]; if (i + 1 != n) cout << deli; } while (br--) cout << endl; }
template <class T> void print(const T& c, const string& deli = " ", int br = 1) { foreach (it, c) { cout << *it; if (++it != c.end()) cout << deli;--it; } while (br--) cout << endl; }
template <class T> void print2d(T a, int w, int h, int width = -1, int br = 1) { for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) {    if (width != -1) cout.width(width); cout << a[i][j] << ' '; } cout << endl; } while (br--) cout << endl; }
   
template <class T> void input(T& a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; }
template <class T> void input(T* a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; }

void fix_pre(int n) { cout.setf(ios::fixed, ios::floatfield); cout.precision(10); }
void fast_io() { cin.tie(0); ios::sync_with_stdio(false); }
#define trace(x) (cout << #x << ": " << (x) << endl)
 
bool in_rect(int x, int y, int w, int h) { return 0 <= x && x < w && 0 <= y && y < h; }


typedef long long ll;
typedef pair<int, int> pint;
 
// y(v): v>^<  y(^): ^>v<
const int dx[] = { 0, 1, 0, -1 };
const int dy[] = { 1, 0, -1, 0 };

const double PI = acos(-1.0);



int main()
{
    fast_io();

    int n, m;
    cin >> n >> m;

    vector<int> g[ten(5)];
    map<pint, int> rela_no; // (a, b)
    rep(i, m)
    {
        int a, b;
        cin >> a >> b;
        --a, --b;
        rela_no[pint(a, b)] = i + 1;
        g[a].pb(b);
    }

    // 
    int dp[ten(5)] = {}; // max len path(x -> i)
    vector<int> prev[ten(5)];
    rep(a, n)
    {
        int nd = dp[a] + 1;
        rep(j, g[a].size())
        {
            int b = g[a][j];
            if (nd > dp[b])
            {
                dp[b] = nd;
                prev[b].clear();
            }
            if (nd == dp[b])
                prev[b].pb(a);
        }
    }


    // 
    vector<int> rela[ten(5) + 10]; // (dis) -> rela_no
    int max_len = *max_element(dp, dp + n);
    bool visit[ten(5)] = {};
    queue<int> q;
    rep(b, n)
    {
        if (dp[b] == max_len)
        {
            q.push(b);
            visit[b] = true;
        }
    }

    while (!q.empty())
    {
        int b = q.front();
        q.pop();
        
        rep(i, prev[b].size())
        {
            int a = prev[b][i];
            rela[dp[b]].pb(rela_no[pint(a, b)]);

            if (!visit[a])
            {
                q.push(a);
                visit[a] = true;
            }
        }
    }

    vector<int> res;
    erep(i, n)
        if (rela[i].size() == 1)
            res.pb(rela[i][0]);
    if (res.empty())
        cout << -1 << endl;
    else
    {
        sort(all(res));
        rep(i, res.size())
            cout << res[i] << endl;
    }
}
accuracy: 1
time_ms: 220
memory_kb: 25848
final_rank: 12

code_snippet: #include<cstdio>
#include<vector>
#include<algorithm>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

const int V_MAX=100000;

struct edge{ int v,id; };

int main(){
	int n,m; scanf("%d%d",&n,&m);
	static vector<edge> G[V_MAX],H[V_MAX];
	rep(i,m){
		int u,v; scanf("%d%d",&u,&v); u--; v--;
		G[u].push_back((edge){v,i});
		H[v].push_back((edge){u,i});
	}

	static int dp1[V_MAX];
	rep(u,n) rep(i,G[u].size()) {
		int v=G[u][i].v;
		dp1[v]=max(dp1[v],dp1[u]+1);
	}

	static int dp2[V_MAX];
	for(int u=n-1;u>=0;u--) rep(i,H[u].size()) {
		int v=H[u][i].v;
		dp2[v]=max(dp2[v],dp2[u]+1);
	}

	int L=*max_element(dp1,dp1+n);

	static int cnt[V_MAX],id[V_MAX];
	rep(u,n) rep(i,G[u].size()) {
		int v=G[u][i].v;
		if(dp1[u]+1+dp2[v]==L) cnt[dp1[u]]++, id[dp1[u]]=G[u][i].id;
	}

	vector<int> ans;
	rep(i,n) if(cnt[i]==1) ans.push_back(id[i]);
	sort(ans.begin(),ans.end());

	if(ans.empty()) puts("-1");
	rep(i,ans.size()) printf("%d\n",ans[i]+1);

	return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 14024
final_rank: 7

code_snippet: #include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
#include <utility>
using namespace std;

typedef pair<int,int> pii;

int main(){
	int n, m;
	scanf("%d%d", &n, &m);
	
	vector<pii> rel(m + n + 1);
	vector<vector<int> > G(n + 1), from(n + 1);
	vector<int> cnt(n + 1);
	for(int i = 1; i <= m; ++i){
		scanf("%d%d", &rel[i].first, &rel[i].second);
		G[rel[i].first].push_back(i);
		++cnt[rel[i].second];
	}
	for(int i = 1; i <= n; ++i){
		rel[i + m] = pii(i, 0);
		G[i].push_back(i + m);
		++cnt[0];
	}

	vector<int> d(n + 1, -1);
	queue<int> q;
	for(int i = 0; i <= n; ++i){
		if(cnt[i] == 0){
			d[i] = 0;
			q.push(i);
		}
	}
	q.push(-1);
	int t = 0;
	while(q.size() > 1){
		int a = q.front();
		q.pop();
		if(a == -1){
			q.push(-1);
			++t;
		}
		else if(d[a] == t){
			for(int i = 0; i < G[a].size(); ++i){
				int u = rel[G[a][i]].second;
				if(d[u] <= t){
					d[u] = t + 1;
					from[u].clear();
				}
				if(d[u] == t + 1){
					from[u].push_back(G[a][i]);
				}
				
				if(--cnt[u] == 0){
					q.push(u);
				}
			}
		}
	}
	
	vector<int> ans;
	q.push(0);
	vector<char> vis(n + 1);
	vis[0] = 1;
	while(q.size() > 1){
		int a = q.front();
		q.pop();
		if(a == -1){
			q.push(-1);
		}
		else{
			if(a != 0 && q.size() == 1 && from[a].size() == 1){
				ans.push_back(from[a][0]);
			}
			
			for(int i = 0; i < from[a].size(); ++i){
				int u = rel[from[a][i]].first;
				if(!vis[u]){
					vis[u] = 1;
					q.push(u);
				}
			}
		}
	}
	
	sort(ans.begin(), ans.end());
	if(ans.empty()){ ans.push_back(-1); }
	for(int i = 0; i < ans.size(); ++i){
		printf("%d\n", ans[i]);
	}
}
accuracy: 1
time_ms: 90
memory_kb: 14944
final_rank: 6

code_snippet: #include <iostream>
#include <map>
#include <cstring>
#include <vector>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;

int n,m;

vector<int> g[100010],rg[100010];
vector< pair<int,int> > e;

int dp[100010];
int f[100010];
vector<int> depth[100010];

map< pair<int,int> , int > pos;
int main(){
	cin >> n >> m;
	for(int i = 0; i < m; i++){
		int a,b;
		scanf("%d%d",&a,&b);
		g[a].push_back(b);
		rg[b].push_back(a);
		pos[make_pair(a,b)] = i;
	}
	
	for(int i = 1; i <= n; i++){
		g[0].push_back(i);
		rg[i].push_back(0);
	}
	
	for(int i = 0; i <= n; i++)
		for(int j = 0; j < g[i].size(); j++)
			dp[g[i][j]] = max(dp[g[i][j]],dp[i]+1);
	
	int maximum = *max_element(dp,dp+100010);
	
	for(int i = n; i >= 0; i--){
		if(maximum == dp[i]) f[i] = 1;
		else if(f[i] == 0) continue;
		for(int j = 0; j < rg[i].size(); j++){
			if(dp[rg[i][j]]+1 == dp[i]){
				f[rg[i][j]] |= f[i];
				e.push_back(make_pair(rg[i][j],i));
			}
		}
	}
	for(int i = 0; i < e.size(); i++){
		depth[dp[e[i].first]].push_back(pos[make_pair(e[i].first,e[i].second)]);
	}
	vector<int> ans;
	for(int i = 1 ; i < 100010 ; i++){
		if( depth[i].size() == 1 ){
			ans.push_back(depth[i][0]+1);
		}
	}
	sort(ans.begin(),ans.end());
	for(int i = 0 ; i < ans.size() ; i++)
		printf("%d\n",ans[i]);
	if( ans.size() == 0 ) cout << -1 << endl;
}
accuracy: 1
time_ms: 200
memory_kb: 27948
final_rank: 13

code_snippet: #include <iostream>
#include <map>
#include <cstring>
#include <vector>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;

int n,m;

vector<int> g[100010],rg[100010];
vector< pair<int,int> > e;

int dp[100010];
int f[100010];
vector<int> depth[100010];

map< pair<int,int> , int > pos;
int main(){
	cin >> n >> m;
	for(int i = 0; i < m; i++){
		int a,b;
		cin >> a >> b;
		g[a].push_back(b);
		rg[b].push_back(a);
		pos[make_pair(a,b)] = i;
	}
	
	for(int i = 1; i <= n; i++){
		g[0].push_back(i);
		rg[i].push_back(0);
	}
	
	for(int i = 0; i <= n; i++)
		for(int j = 0; j < g[i].size(); j++)
			dp[g[i][j]] = max(dp[g[i][j]],dp[i]+1);
	
	int maximum = *max_element(dp,dp+100010);
	
	for(int i = n; i >= 0; i--){
		if(maximum == dp[i]) f[i] = 1;
		else if(f[i] == 0) continue;
		for(int j = 0; j < rg[i].size(); j++){
			if(dp[rg[i][j]]+1 == dp[i]){
				f[rg[i][j]] |= f[i];
				e.push_back(make_pair(rg[i][j],i));
			}
		}
	}
	for(int i = 0; i < e.size(); i++){
		depth[dp[e[i].first]].push_back(pos[make_pair(e[i].first,e[i].second)]);
	}
	vector<int> ans;
	for(int i = 1 ; i < 100010 ; i++){
		if( depth[i].size() == 1 ){
			ans.push_back(depth[i][0]+1);
		}
	}
	sort(ans.begin(),ans.end());
	for(int i = 0 ; i < ans.size() ; i++)
		cout << ans[i] << endl;
	if( ans.size() == 0 ) cout << -1 << endl;
}
accuracy: 1
time_ms: 280
memory_kb: 27888
final_rank: 16

code_snippet: #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
struct Edge{
  int dst, index;
  bool b;
  Edge(int d) : dst(d) {}
  Edge(int d, int idx) : dst(d), index(idx), b(false) {}
};
typedef vector<Edge> Node;
typedef vector<Node> Graph;
void update(int& x, int y){ if(x < y) x = y; }

int main(){
  int N, M;
  while(cin >> N >> M){
    Graph G(N);
    Graph G_inv(N);
    REP(i, M){
      int a, b; cin >> a >> b;
      a--; b--;
      G[a].push_back(Edge(b, i + 1));
      G_inv[b].push_back(Edge(a, i + 1));
    }
    vector<int> dp(N);
    REP(u, N){
      FORIT(e, G[u]){
        update(dp[e->dst], dp[u] + 1);
      }
    }
    int max_height = 0;
    REP(i, N) update(max_height, dp[i]);
    vector<bool> use(N);
    REP(i, N) if(max_height == dp[i]) use[i] = true;
    map<int, int> m;
    for(int i = N - 1; i >= 0; i--)if(use[i]){
      FORIT(e, G_inv[i]){
        if(dp[e->dst] + 1 == dp[i]){
          e->b = true;
          m[dp[e->dst]]++;
          use[e->dst] = true;
        }
      }
    }
    vector<int> ans;
    REP(i, N)FORIT(e, G_inv[i])if(e->b && m[dp[e->dst]] == 1){
      ans.push_back(e->index);
    }
    sort(ans.begin(), ans.end());
    if(ans.empty()){
      cout << -1 << endl;
    }else{
      REP(i, ans.size()) cout << ans[i] << endl;
    }
  }
  return 0;
}
accuracy: 1
time_ms: 220
memory_kb: 17704
final_rank: 10