constructive algorithms graphs shortest paths *1600

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ld long double
#define ll long long
#define S second
#define F first

using namespace __gnu_pbds;
using namespace std;
 
typedef tree<long long, null_type, less_equal<long long>,
    rb_tree_tag, tree_order_statistics_node_update> Tree;
 
const ll INF = 9223372036854775807LL;
const ll inf = 2147483647;
const ll MAXN = 200010;
const ll MOD = 1e9 + 7;
const ld PI = acos(-1);
const ll NROOT = 320;
 
ll binpow(ll a, ll b) {
  ll res = 1;
  for (;b; b /= 2, a *= a, a %= MOD)
    if (b & 1) res *= a, res %= MOD;
  return res % MOD;
}

ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a * b / gcd(a, b);}
ll invmod(ll a) {return binpow(a, MOD - 2);}

vector<bool> s(1e6 + 1, 1);

void sieve() {
  s[0] = s[1] = 0;

  for (ll i = 2; i * i <= 1e6; i ++) {
    if (s[i]) {
      for (ll j = i * i; j <= 1e6; j += i) {
        assert(j > 0 && j <= 1e6);
        s[j] = 0;
      }
    }
  }
}

int32_t main () {
  ios_base::sync_with_stdio(false); 
  cin.tie(nullptr);
  sieve();

  int n, m; cin >> n >> m;
  vector<vector<pair<int, int>>> ans(n + 1);
  ans[1].push_back({n, 2});

  int spt = 2;
  for (int i = 2; i < n; i ++) {
    ans[1].push_back({i, 1});
    spt ++;
  }
  
  while (!s[spt]) {
    spt ++;
    ans[1][1].S ++;
  }

  m -= (n - 1);
  for (int i = 2; i <= n && m; i ++) {
    for (int j = i + 1; j <= n && m; j ++) {
      ans[i].push_back({j, spt + 1});
      m --;
    }
  }

  cout << 2 << " " << spt << "\n";
  for (int i = 1; i <= n; i ++) {
    for (auto &x : ans[i]) {
      assert(x.S <= spt + 1);
      cout << i << " " << x.F << " " << x.S << "\n";
    }
  }

  return 0;
} 
   		 	 		 		 		      	     		


Comments

Submit
0 Comments
More Questions

84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship