結果

問題 No.650 行列木クエリ
ユーザー IL_mstaIL_msta
提出日時 2018-02-10 00:07:25
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 3,657 bytes
コンパイル時間 1,672 ms
コンパイル使用メモリ 137,464 KB
実行使用メモリ 57,088 KB
最終ジャッジ日時 2024-04-17 13:12:12
合計ジャッジ時間 7,820 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,752 KB
testcase_01 AC 60 ms
12,800 KB
testcase_02 AC 260 ms
51,712 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 59 ms
12,800 KB
testcase_05 AC 261 ms
51,712 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1,737 ms
12,800 KB
testcase_09 TLE -
testcase_10 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma region include
#include <iostream>
#include <iomanip>
#include <stdio.h>

#include <sstream>
#include <algorithm>
#include <iterator>
#include <cmath>
#include <complex>

#include <string>
#include <cstring>
#include <vector>
#include <tuple>
#include <bitset>

#include <queue>
#include <set>
#include <map>
#include <stack>
#include <list>

#include <fstream>
#include <random>
//#include <time.h>
#include <ctime>
#pragma endregion //#include
/////////
#define REP(i, x, n) for(int i = x; i < n; ++i)
#define rep(i,n) REP(i,0,n)
#define ALL(X) X.begin(), X.end()
/////////
#pragma region typedef
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef std::pair<LL,LL> PLL;//
typedef std::pair<int,int> PII;//
#pragma endregion //typedef
////定数
const int INF = (int)1e9;
const LL MOD = (LL)1e9+7;
const LL LINF = (LL)4e18+20;
const LD PI = acos(-1.0);
const double EPS = 1e-9;
/////////
using namespace::std;
/////////

void solve(){
	int N;
	cin >> N;
	vector< vector<int> > base(N),tree(N);
	map<int,vector<int> > edgeUV;
	for(int i=0;i<N-1;++i){
		int a,b;
		cin >> a >> b;
		base[a].push_back( b );
		base[b].push_back( a );
		vector<int> temp(2);
		temp[0] = min(a,b);
		temp[1] = max(a,b);
		edgeUV[i] = temp;
	}

	vector<int> oya(N,-1);//i頂点の親
	vector<int> edgeC(N,-1);//i番目の辺の子方向の頂点番号
	vector<bool> use(N,false);
	queue<int> que;
	que.push( 0 );
	map< vector<int>,int > edgeNo;//(u,v)->No
	while( que.size() ){
		int v = que.front();
		que.pop();
		if( use[v] ){
			continue;
		}
		use[v] = true;
		int size = base[v].size();
		for(int i=0;i<size;++i){
			int u = base[v][i];
			if( use[u] ){
				continue;
			}
			tree[u].push_back( v );
			oya[u] = v;//uの親はv
			vector<int> dat(2);
			dat[0] = min(u,v);
			dat[1] = max(u,v);
			edgeNo[dat] = u;
			que.push( u );
		}
	}
	vector< vector< vector<LL> > > X(N,vector< vector<LL> >(2,vector<LL>(2,0)));
	for(int i=0;i<N;++i){
		X[i][0][0] = 1;
		X[i][1][1] = 1;
	}
	///////////
	char type;
	int Q;
	cin >> Q;
	int to,from;
	int pos,x00,x01,x10,x11;
	vector<int> edgeTemp;
	vector< vector<LL> > ret(2,vector<LL>(2,0));
	vector< vector<LL> > temp(2,vector<LL>(2,0));


	while(Q--){
		cin >> type;
		if( type == 'x' ){
			cin >> pos >> x00 >> x01 >> x10 >> x11;
			edgeTemp = edgeUV[pos];
			if( oya[ edgeTemp[0] ] == edgeTemp[1] ){
				pos = edgeTemp[0];
			}else{
				pos = edgeTemp[1];
			}
			X[pos][0][0] = x00;
			X[pos][0][1] = x01;
			X[pos][1][0] = x10;
			X[pos][1][1] = x11;
		}else{
			
			cin >> to >> from;
			
			ret[0][0] = 1;
			ret[0][1] = 0;
			ret[1][0] = 0;
			ret[1][1] = 1;
			pos = from;
			while(pos != to){
				//ret = X[pos] * ret;
				if(true){
					temp[0][0] = X[pos][0][0]*ret[0][0]+X[pos][0][1]*ret[1][0];
					temp[0][1] = X[pos][0][0]*ret[0][1]+X[pos][0][1]*ret[1][1];
					temp[1][0] = X[pos][1][0]*ret[0][0]+X[pos][1][1]*ret[1][0];
					temp[1][1] = X[pos][1][0]*ret[0][1]+X[pos][1][1]*ret[1][1];
					//if(temp[0][0]>= MOD)
						temp[0][0] %= MOD;
					//if(temp[0][1]>= MOD)
						temp[0][1] %= MOD;
					//if(temp[1][0]>= MOD)
						temp[1][0] %= MOD;
					//if(temp[1][1]>= MOD)
						temp[1][1] %= MOD;
					ret = temp;
				}
				if( pos == to ){
					break;
				}
				pos = tree[pos][0];
			}
			cout << ret[0][0] << " " << ret[0][1] << " "
			<< ret[1][0] << " " << ret[1][1] << '\n';
		}
	}
	cout << flush;
}

#pragma region main
signed main(void){
	std::cin.tie(0);
	std::ios::sync_with_stdio(false);
	std::cout << std::fixed;//小数を10進数表示
	cout << setprecision(16);//小数点以下の桁数を指定//coutとcerrで別	

	solve();
}
#pragma endregion //main()
0