AhmedAshraff

Untitled

Aug 1st, 2025
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4. typedef long long ll;
  5. #define sz(s) (int)(s).size()
  6. #define all(s) s.begin(),s.end()
  7.  
  8. void Speed() {
  9.     ios_base::sync_with_stdio(false);
  10.     cin.tie(NULL);
  11. }
  12.  
  13. void solve() {
  14.     int n, m; cin >> n >> m;
  15.     for(int i = 0; i < m; i++){
  16.         int u, v; cin >> u >> v;
  17.     }
  18.     if(m < n - 1 || m > 1ll * n * (n - 1) / 2) return cout << "-1\n", void();
  19.     ll ans = 0;
  20.     for(int i = 2; i <= n; i++) ans += i + 1;
  21.     int nxt = 2;
  22.     for(int j = n - 1, a = nxt, c = 5; j < m; j++){
  23.         if(c - a <= a){
  24.             c++;
  25.             while(c - nxt > n) nxt++;
  26.             a = nxt;
  27.             assert(c - a > a);
  28.         }
  29.         ans += c; a++;
  30.     }
  31.     cout << ans << '\n';
  32. }
  33.  
  34. int main() {
  35.     Speed();
  36.     int tc = 1;
  37.     cin >> tc;
  38.     while (tc--) {
  39.         solve();
  40.     }
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment