提出 #494537


ソースコード 拡げる

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i,n) for(int i = 0; i < (n); i++)
#define sz(c) ((int)c.size())
#define ten(c) ((int)1e##c)

class UF{
	int n;
	vector<int> d;
	public:
	UF(int n) : n(n) , d(n, -1){}
	bool same(int a,int b) { return find(a) == find(b); }
	int find(int a) { return d[a] < 0 ? a :  (d[a] = find(d[a])) ; }
	bool unite(int a,int b ){
		a = find(a),b = find(b);
		if(a==b)return false;
		if(d[a] > d[b]) swap(a,b);
		d[a] += d[b];
		d[b] = a;
		n--;
		return true;
	}
	int size() { return n; }
	
};

typedef pair<int,int > Pii;

int c[ten(5)];
vector<int> e[ten(5)];
int ans[ten(5)],maxes[ten(5)];

int main(){
	int n,m;
	vector<Pii> edges;
	while(cin>>n){
		memset(c,0,sizeof(c));
		FOR(i,n) e[i].clear();
		memset(ans,0,sizeof(ans));
		memset(maxes,0,sizeof(maxes));
		FOR(i,n) cin>>c[i];
		cin>>m;
		FOR(i,m) {
			int a,b; cin>>a>>b;
			a--; b--;
			edges.emplace_back(a,b);
			e[a].push_back(b);
			e[b].push_back(a);
		}
			
		UF uf(n);
		for(auto kv : edges) if(c[kv.first] == c[kv.second]) uf.unite(kv.first,kv.second);
		FOR(i,n) {
			sort(e[i].begin(),e[i].end(),[](const int& a,const int &b){
				return c[a] < c[b];
			});
			FOR(j,sz(e[i])-1){
				int x = e[i][j],y = e[i][j+1];
				if(c[x] == c[y]) {
					uf.unite(x,y);
				}
			}
		}
		
		map<int,vector<int>> mp;
		FOR(i,n) mp[uf.find(i)].push_back(i);
		vector<int> ids;
		for(auto x : mp) ids.push_back(x.first);
		sort(ids.begin(),ids.end(),[](const int&a,const int& b){
			return c[a] < c[b];
		});

		for(auto u : ids) {
			auto& grp = mp[u];
			int mx = 0;
			for(auto x : grp) {
				mx = max(mx,maxes[x]);
				for(auto to : e[x]) mx = max(mx,maxes[to]);
			}
			
			//set anses
			for(auto x : grp) ans[x] = mx + 1;
			
			//set maxes
			for(auto x : grp) {
				maxes[x] = ans[x];
				for(auto to : e[x]) maxes[to] = ans[x];
			}
		}
		
		ll out = 0;
		FOR(i,n) out += ans[i];
		
		cout << out << endl;
	}
	return 0;
}

提出情報

提出日時
問題 J - Black Company
ユーザ dochikoku
言語 C++11 (GCC 4.8.1)
得点 100
コード長 2048 Byte
結果 AC
実行時間 576 ms
メモリ 21660 KiB

ジャッジ結果

セット名 All
得点 / 配点 100 / 100
結果
AC × 62
セット名 テストケース
All 00_sample_00, 00_sample_01, 00_sample_02, 00_sample_03, 00_sample_04, 10_rand_00, 10_rand_01, 10_rand_02, 10_rand_03, 10_rand_04, 11_max_rand_00, 11_max_rand_01, 11_max_rand_02, 11_max_rand_03, 11_max_rand_04, 20_path_00, 20_path_01, 20_path_02, 20_path_03, 20_path_04, 21_max_path_00, 21_max_path_01, 21_max_path_02, 21_max_path_03, 21_max_path_04, 30_star_00, 30_star_01, 30_star_02, 30_star_03, 30_star_04, 31_max_star_00, 31_max_star_01, 31_max_star_02, 31_max_star_03, 31_max_star_04, 40_clique_00, 40_clique_01, 40_clique_02, 40_clique_03, 40_clique_04, 41_max_clique_00, 41_max_clique_01, 41_max_clique_02, 41_max_clique_03, 41_max_clique_04, 50_discon_00, 50_discon_01, 50_discon_02, 50_discon_03, 50_discon_04, 51_max_discon_00, 51_max_discon_01, 51_max_discon_02, 51_max_discon_03, 51_max_discon_04, 60_noedge_00, 60_noedge_01, 60_noedge_02, 60_noedge_03, 60_noedge_04, 61_max_noedge_00, 61_min_noedge
ケース名 結果 実行時間 メモリ
00_sample_00 AC 35 ms 4320 KiB
00_sample_01 AC 32 ms 4256 KiB
00_sample_02 AC 30 ms 4272 KiB
00_sample_03 AC 32 ms 4260 KiB
00_sample_04 AC 32 ms 4376 KiB
10_rand_00 AC 159 ms 6808 KiB
10_rand_01 AC 505 ms 18704 KiB
10_rand_02 AC 342 ms 18584 KiB
10_rand_03 AC 142 ms 10648 KiB
10_rand_04 AC 125 ms 11676 KiB
11_max_rand_00 AC 333 ms 18720 KiB
11_max_rand_01 AC 576 ms 21660 KiB
11_max_rand_02 AC 264 ms 16552 KiB
11_max_rand_03 AC 341 ms 18844 KiB
11_max_rand_04 AC 411 ms 20036 KiB
20_path_00 AC 32 ms 4388 KiB
20_path_01 AC 279 ms 15636 KiB
20_path_02 AC 92 ms 7572 KiB
20_path_03 AC 137 ms 9624 KiB
20_path_04 AC 138 ms 10144 KiB
21_max_path_00 AC 400 ms 20376 KiB
21_max_path_01 AC 417 ms 20376 KiB
21_max_path_02 AC 419 ms 20376 KiB
21_max_path_03 AC 395 ms 20384 KiB
21_max_path_04 AC 346 ms 20124 KiB
30_star_00 AC 209 ms 13592 KiB
30_star_01 AC 290 ms 16668 KiB
30_star_02 AC 299 ms 16536 KiB
30_star_03 AC 48 ms 5280 KiB
30_star_04 AC 204 ms 8848 KiB
31_max_star_00 AC 379 ms 20760 KiB
31_max_star_01 AC 394 ms 20756 KiB
31_max_star_02 AC 396 ms 20764 KiB
31_max_star_03 AC 351 ms 16404 KiB
31_max_star_04 AC 222 ms 9492 KiB
40_clique_00 AC 154 ms 8336 KiB
40_clique_01 AC 38 ms 4520 KiB
40_clique_02 AC 33 ms 4256 KiB
40_clique_03 AC 201 ms 8140 KiB
40_clique_04 AC 32 ms 4260 KiB
41_max_clique_00 AC 206 ms 8468 KiB
41_max_clique_01 AC 224 ms 8468 KiB
41_max_clique_02 AC 228 ms 8460 KiB
41_max_clique_03 AC 227 ms 8464 KiB
41_max_clique_04 AC 232 ms 8468 KiB
50_discon_00 AC 46 ms 5152 KiB
50_discon_01 AC 227 ms 11796 KiB
50_discon_02 AC 399 ms 18968 KiB
50_discon_03 AC 196 ms 10652 KiB
50_discon_04 AC 334 ms 17548 KiB
51_max_discon_00 AC 359 ms 19728 KiB
51_max_discon_01 AC 352 ms 19736 KiB
51_max_discon_02 AC 419 ms 20504 KiB
51_max_discon_03 AC 341 ms 18456 KiB
51_max_discon_04 AC 361 ms 20236 KiB
60_noedge_00 AC 48 ms 5672 KiB
60_noedge_01 AC 144 ms 11300 KiB
60_noedge_02 AC 85 ms 8352 KiB
60_noedge_03 AC 211 ms 15012 KiB
60_noedge_04 AC 124 ms 11420 KiB
61_max_noedge_00 AC 233 ms 16276 KiB
61_min_noedge AC 33 ms 4376 KiB