Submission #783372


Source Code Expand

#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define pb push_back
#define dbg(...) do{cerr<<__LINE__<<": ";dbgprint(#__VA_ARGS__, __VA_ARGS__);}while(0);

using namespace std;

namespace std{template<class S,class T>struct hash<pair<S,T>>{size_t operator()(const pair<S,T>&p)const{return ((size_t)1e9+7)*hash<S>()(p.first)+hash<T>()(p.second);}};template<class T>struct hash<vector<T>>{size_t operator()(const vector<T> &v)const{size_t h=0;for(auto i : v)h=h*((size_t)1e9+7)+hash<T>()(i)+1;return h;}};}
template<class T>ostream& operator<<(ostream &os, const vector<T> &v){os<<"[ ";rep(i,v.size())os<<v[i]<<(i==v.size()-1?" ]":", ");return os;}template<class T>ostream& operator<<(ostream &os,const set<T> &v){os<<"{ "; for(const auto &i:v)os<<i<<", ";return os<<"}";}
template<class T,class U>ostream& operator<<(ostream &os,const map<T,U> &v){os<<"{";for(const auto &i:v)os<<" "<<i.first<<": "<<i.second<<",";return os<<"}";}template<class T,class U>ostream& operator<<(ostream &os,const pair<T,U> &p){return os<<"("<<p.first<<", "<<p.second<<")";}
void dbgprint(const string &fmt){cerr<<endl;}template<class H,class... T>void dbgprint(const string &fmt,const H &h,const T&... r){cerr<<fmt.substr(0,fmt.find(","))<<"= "<<h<<" ";dbgprint(fmt.substr(fmt.find(",")+1),r...);}
typedef long long ll;typedef vector<int> vi;typedef pair<int,int> pi;const int inf = (int)1e9;const double INF = 1e12, EPS = 1e-9;

const int N = 100000;
int n;
vector<set<int>> e;
vector<pair<pi, int>> es;

int sz, id[N], to[N];
set<pi> s[N]; //(cost, id)
/*
int rec_sz(int c, int p){
	int res = 1;
	for(int i : e[c]){
		int s = es[i].first.first, t = es[i].first.second;
		if(c == t) swap(s, t);
		if(t == p) continue;
		res += rec_sz(t, c);
	}
	return res;
}
*/
void rec(set<pi> &s1, set<pi> &s2, int c, int p){
	for(int i : e[c]){
		int s = es[i].first.first, t = es[i].first.second;
		if(t == c) swap(s, t);
		if(t == p) continue;
		s2.insert(mp(es[i].second, i));
		s1.erase(mp(es[i].second, i));
		to[i] = sz;
		rec(s1, s2, t, c);
	}
}

int main(){
	cin.tie(0); cin.sync_with_stdio(0);
	
	cin >> n;
	e.resize(n);
	rep(i, n - 1){
		int a, b, c; cin >> a >> b >> c; a--; b--;
		e[a].insert(i);
		e[b].insert(i);
		es.pb(mp(mp(a, b), c));
		s[0].insert(mp(c, i));
	}
	sz++;
	
	int q; cin >> q;
	vi avis(n), bvis(n);
	
	while(q--){
		int t, idx, d; cin >> t >> idx; idx--;
		if(t == 1){
			cin >> d;
			if(es[idx].second == -inf) continue;
			s[to[idx]].erase(mp(es[idx].second, idx)); es[idx].second += d;
			s[to[idx]].insert(mp(es[idx].second, idx));
			continue;
		}
		//rep(i, es.size()) dbg(es[i]); cerr<<endl;
		if(es[idx].second == -inf){ puts("-1"); continue; }
		
		pi mn = *s[to[idx]].begin();
		idx = mn.second;
		printf("%d\n", idx + 1);
		
		int a = es[idx].first.first, b = es[idx].first.second;
		e[a].erase(idx);
		e[b].erase(idx);
		//int sa = rec_sz(a, a), sb = rec_sz(b, b);
		vi av(1, a), bv(1, b);
		int ap = a, bp = b, asml = 1, af = 0, bf = 0;
		auto ae = e[ap].begin(), be = e[bp].begin();
		
		queue<int> qa, qb;
		qa.push(a); qb.push(b);
		
		while(1){
			BACKA:
			if(ae == e[ap].end()){
				af = 1;
				if(qa.empty()) break;
				ap = qa.front(); qa.pop();
			}
			{
				if(af) ae = e[ap].begin(), af = 0; if(ae == e[ap].end()) goto BACKA;
				int s = es[*ae].first.first, t = es[*ae].first.second;
				if(t == a) swap(s, t); ++ae;
				if(avis[t]) goto BACKA;
				qa.push(t); avis[t] = 1; av.pb(t);
			}
			BACKB:
			if(be == e[bp].end()){
				bf = 1;
				if(qb.empty()){ asml = 0; break; }
				bp = qb.front(); qb.pop();
			}
			{
				if(bf) be = e[bp].begin(), bf = 0; if(be == e[bp].end()) goto BACKB;
				int s = es[*be].first.first, t = es[*be].first.second;
				if(t == b) swap(s, t); ++be;
				if(bvis[t]) goto BACKB;
				qb.push(t); bvis[t] = 1; bv.pb(t);
			}
		}
		for(int i : av) avis[i] = 0;
		for(int i : bv) bvis[i] = 0;
		
		if(!asml) swap(a, b);
		
		s[to[idx]].erase(mp(es[idx].second, idx));
		es[idx].second = -inf;
		rec(s[to[idx]], s[sz], a, a);
		sz++;
	}
	return 0;
}

Submission Info

Submission Time
Task I - 盆栽
User nadeko
Language C++11 (GCC 4.9.2)
Score 300
Code Size 4206 Byte
Status AC
Exec Time 2158 ms
Memory 26732 KB

Judge Result

Set Name Subtask All
Score / Max Score 30 / 30 270 / 270
Status
AC × 17
AC × 50
Set Name Test Cases
Subtask atetubou-challenge1.in, subtask_00.txt, subtask_01.txt, subtask_02.txt, subtask_03.txt, subtask_04.txt, subtask_05.txt, subtask_06.txt, subtask_07.txt, subtask_08.txt, subtask_09.txt, subtask_10.txt, subtask_11.txt, subtask_12.txt, subtask_13.txt, subtask_14.txt, subtask_15.txt, subtask_16.txt
All scrambled_00.txt, scrambled_01.txt, scrambled_02.txt, scrambled_03.txt, scrambled_04.txt, scrambled_05.txt, scrambled_06.txt, scrambled_07.txt, scrambled_08.txt, scrambled_09.txt, scrambled_10.txt, scrambled_11.txt, scrambled_12.txt, scrambled_13.txt, scrambled_14.txt, scrambled_15.txt, scrambled_16.txt, scrambled_17.txt, scrambled_18.txt, scrambled_19.txt, scrambled_20.txt, scrambled_21.txt, scrambled_22.txt, scrambled_23.txt, scrambled_24.txt, scrambled_25.txt, scrambled_26.txt, scrambled_27.txt, scrambled_28.txt, scrambled_29.txt, scrambled_30.txt, scrambled_31.txt, scrambled_32.txt, subtask_00.txt, subtask_01.txt, subtask_02.txt, subtask_03.txt, subtask_04.txt, subtask_05.txt, subtask_06.txt, subtask_07.txt, subtask_08.txt, subtask_09.txt, subtask_10.txt, subtask_11.txt, subtask_12.txt, subtask_13.txt, subtask_14.txt, subtask_15.txt, subtask_16.txt
Case Name Status Exec Time Memory
scrambled_00.txt AC 113 ms 5620 KB
scrambled_01.txt AC 39 ms 5796 KB
scrambled_02.txt AC 39 ms 5800 KB
scrambled_03.txt AC 39 ms 5804 KB
scrambled_04.txt AC 39 ms 5804 KB
scrambled_05.txt AC 39 ms 5804 KB
scrambled_06.txt AC 39 ms 5792 KB
scrambled_07.txt AC 39 ms 5804 KB
scrambled_08.txt AC 39 ms 5800 KB
scrambled_09.txt AC 39 ms 5808 KB
scrambled_10.txt AC 39 ms 5808 KB
scrambled_11.txt AC 1297 ms 26732 KB
scrambled_12.txt AC 1020 ms 26708 KB
scrambled_13.txt AC 878 ms 26732 KB
scrambled_14.txt AC 1496 ms 26716 KB
scrambled_15.txt AC 840 ms 26728 KB
scrambled_16.txt AC 1306 ms 26652 KB
scrambled_17.txt AC 978 ms 26712 KB
scrambled_18.txt AC 1076 ms 26728 KB
scrambled_19.txt AC 1322 ms 26640 KB
scrambled_20.txt AC 1522 ms 26632 KB
scrambled_21.txt AC 361 ms 26340 KB
scrambled_22.txt AC 40 ms 5796 KB
scrambled_23.txt AC 41 ms 5840 KB
scrambled_24.txt AC 39 ms 5800 KB
scrambled_25.txt AC 44 ms 5872 KB
scrambled_26.txt AC 41 ms 5808 KB
scrambled_27.txt AC 1644 ms 26724 KB
scrambled_28.txt AC 798 ms 26704 KB
scrambled_29.txt AC 1038 ms 26720 KB
scrambled_30.txt AC 1077 ms 26632 KB
scrambled_31.txt AC 2158 ms 26720 KB
scrambled_32.txt AC 36 ms 5552 KB
subtask_00.txt AC 37 ms 5548 KB
subtask_01.txt AC 39 ms 5800 KB
subtask_02.txt AC 39 ms 5804 KB
subtask_03.txt AC 39 ms 5792 KB
subtask_04.txt AC 43 ms 5856 KB
subtask_05.txt AC 39 ms 5812 KB
subtask_06.txt AC 37 ms 5804 KB
subtask_07.txt AC 38 ms 5804 KB
subtask_08.txt AC 39 ms 5804 KB
subtask_09.txt AC 39 ms 5820 KB
subtask_10.txt AC 39 ms 5792 KB
subtask_11.txt AC 51 ms 5800 KB
subtask_12.txt AC 39 ms 5800 KB
subtask_13.txt AC 39 ms 5808 KB
subtask_14.txt AC 39 ms 5800 KB
subtask_15.txt AC 38 ms 5828 KB
subtask_16.txt AC 34 ms 5544 KB