Submission #411305

# Submission time Handle Problem Language Result Execution time Memory
411305 2021-05-25T01:40:11 Z islander7 Dreaming (IOI13_dreaming) C++17
40 / 100
1000 ms 38856 KB
#include "dreaming.h"
#include <iostream>
#include <queue>
#include <map>
#include <vector>
#include <algorithm>
#define ll long long
#define pii pair <ll, int>
using namespace std;
int travelTime(int N, int M, int L, int A[], int B[], int T[])
{
	map <int, vector <pii>> adj;
	map <int, vector <int>> cc;
	map <pii, int> edges;
	map <int, int> ccnum;
	int maxgrp;
	ll maxlen = 0;
	ll m1 = -1, m2 = -1, m3 = -1;
	for (int i = 0; i < M; i++)
	{
		adj[A[i]].push_back(make_pair(B[i], T[i]));
		adj[B[i]].push_back(make_pair(A[i], T[i]));
		edges[make_pair(A[i], B[i])] = T[i];
		edges[make_pair(B[i], A[i])] = T[i];
	}
	int cur = 0;
	for (int i = 0; i < N; i++)
	{
		ccnum[i] = -1;
	}
	for (int i = 0; i < N; i++)
	{
		if (ccnum[i] == -1)
		{
			queue <int> curs;
			curs.push(i);
			ccnum[i] = cur;
			while (!curs.empty())
			{
				int a = curs.front();
				curs.pop();
				for (auto x : adj[a])
				{
					if (ccnum[x.first] == -1)
					{
						ccnum[x.first] = cur;
						curs.push(x.first);
					}
				}
			}
			cur++;
		}
	}
	for (int i = 0; i < N; i++)
	{
		cc[ccnum[i]].push_back(i);
	}
	maxgrp = cur - 1;
	for (int i = 0; i <= maxgrp; i++)
	{
		if (cc[i].size() == 1)
		{
			if (m3 < 0)
			{
				m3 = 0;
				if (m3 > m2)
				{
					swap(m3, m2);
					if (m2 > m1)
					{
						swap(m2, m1);
					}
				}
			}
			continue;
		}
		map <int, ll> mins;
		for (int a : cc[i])
		{
			mins[a] = -1;
		}
		mins[cc[i][0]] = 0;
		queue <int> curs;
		curs.push(cc[i][0]);
		while (!curs.empty())
		{
			int a = curs.front();
			curs.pop();
			for (auto b : adj[a])
			{
				if (mins[b.first] == -1)
				{
					curs.push(b.first);
					mins[b.first] = mins[a] + edges[make_pair(a, b.first)];
				}
			}
		}
		int maxl = 0;
		int far = 0;
		for (int a : cc[i])
		{
			if (mins[a] > maxl)
			{
				maxl = mins[a];
				far = a;
			}
		}
		map <int, int> parent;
		for (int a : cc[i])
		{
			mins[a] = -1;
		}
		mins[far] = 0;
		curs.push(far);
		while (!curs.empty())
		{
			int a = curs.front();
			curs.pop();
			for (auto b : adj[a])
			{
				if (mins[b.first] == -1)
				{
					curs.push(b.first);
					mins[b.first] = mins[a] + edges[make_pair(a, b.first)];
					parent[b.first] = a;
				}
			}
		}
		maxl = 0;
		int far2 = 0;
		for (int a : cc[i])
		{
			if (mins[a] > maxl)
			{
				maxl = mins[a];
				far2 = a;
			}
		}
		int start = far;
		int end = far2;
		ll length = mins[far2];
		ll tot = 0;
		if (length > maxlen)
		{
			maxlen = length;
		}
		int curpos = far2;
		int minmax = 0;
		while (tot * 2 < length)
		{
			tot += edges[make_pair(curpos, parent[curpos])];
			if (tot * 2 == length)
			{
				minmax = tot;
			}
			if (tot * 2 > length)
			{
				if (tot * 2 - edges[make_pair(curpos, parent[curpos])] >= length)
				{
					minmax = length - tot + edges[make_pair(curpos, parent[curpos])];
				}
				else
				{
					minmax = tot;
				}
			}
			curpos = parent[curpos];
		}
		if (m3 < minmax)
		{
			m3 = minmax;
			if (m3 > m2)
			{
				swap(m3, m2);
				if (m2 > m1)
				{
					swap(m2, m1);
				}
			}
		}
	}
	if (m2 == -1)
	{
		return max(maxlen, m1);
	}
	else if (m3 == -1)
	{
		return max(maxlen, m1 + L + m2);
	}
	else
	{
		return max(maxlen, max(m1 + L + m2, m2 + m3 + 2 * L));
	}
}

Compilation message

dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:139:7: warning: unused variable 'start' [-Wunused-variable]
  139 |   int start = far;
      |       ^~~~~
dreaming.cpp:140:7: warning: unused variable 'end' [-Wunused-variable]
  140 |   int end = far2;
      |       ^~~
# Verdict Execution time Memory Grader output
1 Execution timed out 1085 ms 38856 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1085 ms 38856 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 328 ms 25132 KB Output is correct
2 Correct 321 ms 25164 KB Output is correct
3 Correct 324 ms 25068 KB Output is correct
4 Correct 326 ms 25152 KB Output is correct
5 Correct 337 ms 25000 KB Output is correct
6 Correct 341 ms 26580 KB Output is correct
7 Correct 345 ms 26264 KB Output is correct
8 Correct 312 ms 24636 KB Output is correct
9 Correct 304 ms 24452 KB Output is correct
10 Correct 334 ms 26152 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 122 ms 23792 KB Output is correct
13 Correct 126 ms 23748 KB Output is correct
14 Correct 126 ms 23748 KB Output is correct
15 Correct 126 ms 23748 KB Output is correct
16 Correct 125 ms 23736 KB Output is correct
17 Correct 124 ms 23660 KB Output is correct
18 Correct 125 ms 23748 KB Output is correct
19 Correct 130 ms 23772 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 3 ms 972 KB Output is correct
23 Correct 121 ms 23768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 4 ms 588 KB Output is correct
27 Correct 7 ms 844 KB Output is correct
28 Correct 11 ms 1260 KB Output is correct
29 Correct 4 ms 588 KB Output is correct
30 Correct 7 ms 844 KB Output is correct
31 Correct 10 ms 1264 KB Output is correct
32 Correct 4 ms 588 KB Output is correct
33 Correct 7 ms 880 KB Output is correct
34 Correct 13 ms 1268 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 1 ms 332 KB Output is correct
37 Correct 1 ms 332 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 1 ms 204 KB Output is correct
41 Correct 1 ms 204 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 1 ms 332 KB Output is correct
45 Correct 1 ms 332 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 1 ms 332 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 1 ms 332 KB Output is correct
50 Correct 1 ms 332 KB Output is correct
51 Correct 1 ms 332 KB Output is correct
52 Correct 1 ms 204 KB Output is correct
53 Correct 1 ms 204 KB Output is correct
54 Correct 14 ms 1548 KB Output is correct
55 Correct 14 ms 1484 KB Output is correct
56 Correct 8 ms 1144 KB Output is correct
57 Correct 5 ms 972 KB Output is correct
58 Correct 13 ms 1408 KB Output is correct
59 Correct 13 ms 1512 KB Output is correct
60 Correct 7 ms 1112 KB Output is correct
61 Correct 6 ms 1032 KB Output is correct
62 Correct 13 ms 1376 KB Output is correct
63 Correct 13 ms 1436 KB Output is correct
64 Correct 4 ms 972 KB Output is correct
65 Correct 8 ms 1100 KB Output is correct
66 Correct 14 ms 1584 KB Output is correct
67 Correct 14 ms 1460 KB Output is correct
68 Correct 9 ms 1176 KB Output is correct
69 Correct 10 ms 1100 KB Output is correct
70 Correct 6 ms 972 KB Output is correct
71 Correct 6 ms 972 KB Output is correct
72 Correct 14 ms 1556 KB Output is correct
73 Correct 13 ms 1484 KB Output is correct
74 Correct 11 ms 1260 KB Output is correct
75 Correct 11 ms 1280 KB Output is correct
76 Correct 13 ms 1416 KB Output is correct
77 Correct 14 ms 1496 KB Output is correct
78 Correct 6 ms 972 KB Output is correct
79 Correct 6 ms 972 KB Output is correct
80 Correct 4 ms 972 KB Output is correct
81 Correct 3 ms 972 KB Output is correct
82 Correct 10 ms 1176 KB Output is correct
83 Correct 10 ms 1236 KB Output is correct
84 Correct 6 ms 1100 KB Output is correct
85 Correct 4 ms 972 KB Output is correct
86 Correct 10 ms 1228 KB Output is correct
87 Correct 13 ms 1448 KB Output is correct
88 Correct 8 ms 1100 KB Output is correct
89 Correct 8 ms 1100 KB Output is correct
90 Correct 7 ms 1100 KB Output is correct
91 Correct 6 ms 972 KB Output is correct
92 Correct 9 ms 1184 KB Output is correct
93 Correct 8 ms 1100 KB Output is correct
94 Correct 12 ms 1100 KB Output is correct
95 Correct 11 ms 1188 KB Output is correct
96 Correct 11 ms 1260 KB Output is correct
97 Correct 11 ms 1280 KB Output is correct
98 Correct 11 ms 1228 KB Output is correct
99 Correct 13 ms 1488 KB Output is correct
100 Correct 12 ms 1356 KB Output is correct
101 Correct 11 ms 1228 KB Output is correct
102 Correct 1 ms 332 KB Output is correct
103 Correct 1 ms 332 KB Output is correct
104 Correct 1 ms 332 KB Output is correct
105 Correct 1 ms 332 KB Output is correct
106 Correct 1 ms 332 KB Output is correct
107 Correct 1 ms 332 KB Output is correct
108 Correct 1 ms 332 KB Output is correct
109 Correct 1 ms 332 KB Output is correct
110 Correct 1 ms 332 KB Output is correct
111 Correct 1 ms 332 KB Output is correct
112 Correct 2 ms 332 KB Output is correct
113 Correct 3 ms 460 KB Output is correct
114 Correct 4 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1085 ms 38856 KB Time limit exceeded
2 Halted 0 ms 0 KB -