606D - Lazy Student - CodeForces Solution


graphs *1700

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <iomanip>
#include <iterator>
#include <cmath>
#include <map>
#include <string.h>
#include <ctime>
#include <queue>
#include<unordered_map>
#include<stack> 
#include<unordered_set>
#define Tamora ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
#define endl '\n'
#define all(a) a.begin(),a.end()
#define ld long double
using namespace std;

vector<int>dirx{ 1,-1, 0,0,1,1,-1,-1 };
vector<int>diry{ 0,0,-1,1,1,-1,1,-1 };

bool valid(int n, int m, int x, int y) {
	return x >= 0 && y >= 0 && x < n&& y < m;
}

ll gcd(ll a, ll b) {
	if (b == 0)
		return a;
	return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
	return (a * b) / gcd(a, b);
}

const ll mod = 1e9, inf = 1e18, N = 3e5 + 5, M = 1e3 + 5;

bool cmp(pair<pair<int, int>, int>a, pair<pair<int, int>, int>b) {
	return a.first.first < b.first.first || (a.first.first == b.first.first && b.first.second < a.first.second);
}

void solve() {
	int n, m;
	cin >> n >> m;
	vector<pair<pair<int, int>,int>>edge(m);
	for (int i = 0; i < m; i++) {
		int a, b;
		cin >> a >> b;
		edge[i] = { { a,b},i };
	}
	sort(all(edge),cmp);
	vector<int>tree{ 1 };
	vector<pair<int,int>>ans(m);
	int nxt = 2;
	vector<pair<int,int>>av;
	int rem = m;
	for (auto i : edge) {
		int b = i.first.second, ind = i.second;
		if (b) {
			for (int i = 2; i <= tree.size() && av.size() < rem; i++)
				av.push_back({ i,nxt });
			tree.push_back(nxt), ans[ind] = { 1,nxt++ };
		}
		else
			if (!av.empty())
				ans[ind] = av.back(), av.pop_back();
			else
				return void(cout << -1);
		rem--;
	}
	for (auto i : ans)
		cout << i.first << ' ' << i.second << endl;
}


//void files() {
//
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
//}


int main() {
	Tamora;
	/*int t; cin >> t;
	while (t--)*/
	solve();

}


Comments

Submit
0 Comments
More Questions

1516B - AGAGA XOOORRR
1515A - Phoenix and Gold
1515B - Phoenix and Puzzle
155A - I_love_username
49A - Sleuth
1541A - Pretty Permutations
1632C - Strange Test
673A - Bear and Game
276A - Lunch Rush
1205A - Almost Equal
1020B - Badge
1353A - Most Unstable Array
770A - New Password
1646B - Quality vs Quantity
80A - Panoramix's Prediction
1354B - Ternary String
122B - Lucky Substring
266B - Queue at the School
1490A - Dense Array
1650B - DIV + MOD
1549B - Gregor and the Pawn Game
553A - Kyoya and Colored Balls
1364A - XXXXX
1499B - Binary Removals
1569C - Jury Meeting
108A - Palindromic Times
46A - Ball Game
114A - Cifera
776A - A Serial Killer
25B - Phone numbers