PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FW0000042. Lược bỏ điều kiện tiên quyết thừa (No More Prerequisites, Please!)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Lược bỏ điều kiện tiên quyết thừa (No More Prerequisites, Please!)

No More Prerequisites, Please!

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

Given a DAG of direct prerequisites, an edge u→vu\to v is redundant if another path with at least one intermediate course exists. Output all nonredundant direct edges in sorted order.

Input

Integers n,mn,m and mm directed edges u,vu,v meaning course uu is a direct prerequisite of course vv.

Output

Print the number of nonredundant prerequisite edges, followed by one u v pair per line in increasing order of u, then v.

Subtasks

  • Subtask 1 (40 points): n≤12n\le12; 2≤n≤1002\le n\le100, 0≤m≤10000\le m\le1000; 1-indexed vertices, unique edges forming a DAG.
  • Subtask 2 (60 points): 2≤n≤1002\le n\le100, 0≤m≤10000\le m\le1000; 1-indexed vertices, unique edges forming a DAG.

Examples

Input

4 4
1 2
2 3
1 3
3 4

Output

3
1 2
2 3
3 4

Explanation

The edge 1 to 3 is redundant because 1 to 2 to 3 already exists. The three remaining edges cannot be replaced by a path through an intermediate course.