fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int tc;
  6. int const NMAX = 1e5;
  7. int n, m;
  8. bool exists[1 + NMAX];
  9.  
  10. int main() {
  11. cin >> tc;
  12. while(tc--) {
  13. cin >> n >> m;
  14. for (int i = 1; i <= n-1; i++) {
  15. exists[i] = false;
  16. }
  17. bool valid = true;
  18. for (int i = 1; i <= m; i++) {
  19. int a, b;
  20. cin >> a >> b;
  21. if (a > b) {
  22. valid = false;
  23. }
  24. if (b == a+1) {
  25. exists[a] = true;
  26. }
  27. }
  28. if (!valid) {
  29. cout << -1 << "\n";
  30. continue;
  31. }
  32. int counter = n-1;
  33. for (int i = 1; i <= n-1; i++) {
  34. if (exists[i]) {
  35. counter--;
  36. }
  37. }
  38. cout << counter << "\n";
  39. }
  40. }
Success #stdin #stdout 0s 5320KB
stdin
3
4 2
1 2
3 4
4 2
1 3
2 4
3 1
3 1
stdout
1
3
-1