Submission #47721

# Submission time Handle Problem Language Result Execution time Memory
47721 2018-05-06T17:56:53 Z dqhungdl Evacuation plan (IZhO18_plan) C++17
54 / 100
4000 ms 45496 KB
#include <bits/stdc++.h>
using namespace std;

const int MAX=17;
typedef pair<int,int> ii;
int n,m,k,T,d[100005],R[100005],h[100005],P[100005][20],mind[100005][20];
vector<ii> Edge,g[100005],gg[100005];
set<ii> s;

bool cmp(ii x1,ii x2)
{
	return min(d[x1.first],d[x1.second])>min(d[x2.first],d[x2.second]);
}

int Find(int u)
{
	if(u==R[u])
		return u;
	return R[u]=Find(R[u]);
}

void Union(int u,int v)
{
	if(u>v)
		R[u]=v;
	else
		R[v]=u;
}

void Enter()
{
	cin>>n>>m;
	int u,v,w;
	while(m--)
	{
		cin>>u>>v>>w;
		Edge.push_back(ii(u,v));
		g[u].push_back(ii(v,w));
		g[v].push_back(ii(u,w));
	}
	for(int i=1;i<=n;i++)
	{
		d[i]=1e9;
		R[i]=i;
	}
	cin>>k;
	while(k--)
	{
		cin>>u;
		s.insert(ii(0,u));
		d[u]=0;
	}
}

void BFS()
{
	while(s.size()>0)
	{
		int u=(*s.begin()).second;
		s.erase(s.begin());
		for(int i=0;i<g[u].size();i++)
		{
			int v=g[u][i].first;
			int w=g[u][i].second;
			if(d[v]>d[u]+w)
			{
				if(d[v]!=1e9)
					s.erase(s.find(ii(d[v],v)));
				d[v]=d[u]+w;
				s.insert(ii(d[v],v));
			}
		}
	}
}

void DFS(int u,int high)
{
	h[u]=high;
	for(int i=0;i<gg[u].size();i++)
	{
		int v=gg[u][i].first;
		int w=gg[u][i].second;
		if(h[v]==0)
		{
			P[v][0]=u;
			mind[v][0]=w;
			DFS(v,high+1);					
		}
	}
}

int LCA(int u,int v)
{
	int rs=1e9;
    for(int k=MAX;k>=0;k--)
        if(h[P[u][k]]>=h[v])
        {
        	rs=min(rs,mind[u][k]);
            u=P[u][k];         
        }
        else
        if(h[P[v][k]]>=h[u])
        {
        	rs=min(rs,mind[v][k]);
            v=P[v][k];
        }
    for(int k=10;k>=0;k--)
        if(P[u][k]!=P[v][k])
        {
        	rs=min(rs,mind[u][k]);
        	rs=min(rs,mind[v][k]);
            u=P[u][k];
            v=P[v][k];
        }
    while(u!=v)
    {
    	rs=min(rs,mind[u][0]);
        rs=min(rs,mind[v][0]);
        u=P[u][0];
        v=P[v][0];
    }
    return rs;
}

void InitTree()
{
	sort(Edge.begin(),Edge.end(),cmp);
	for(int i=0;i<Edge.size();i++)
	{
		int u=Edge[i].first;
		int v=Edge[i].second;
		int w=min(d[u],d[v]);
		if(Find(u)!=Find(v))
		{
			Union(Find(u),Find(v));
			gg[u].push_back(ii(v,w));
			gg[v].push_back(ii(u,w));
		}
	}
	DFS(1,1);
	for(int k=1;k<=MAX;k++)
    	for(int i=1;i<=n;i++)
    	{
			P[i][k]=P[P[i][k-1]][k-1];
			mind[i][k]=min(mind[i][k-1],mind[P[i][k-1]][k-1]);
    	}
}

void Query()
{
	cin>>T;
	int u,v;
	while(T--)
	{
		cin>>u>>v;
		cout<<LCA(u,v)<<"\n";
	}
}

int main()
{
	ios_base::sync_with_stdio(false);
	//freopen("TEST.INP","r",stdin);
	//freopen("TEST.OUT","w",stdout);
	Enter();
	BFS();
	InitTree();
	Query();
}

Compilation message

plan.cpp: In function 'void BFS()':
plan.cpp:61:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=0;i<g[u].size();i++)
               ~^~~~~~~~~~~~
plan.cpp: In function 'void DFS(int, int)':
plan.cpp:79:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<gg[u].size();i++)
              ~^~~~~~~~~~~~~
plan.cpp: In function 'void InitTree()':
plan.cpp:128:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<Edge.size();i++)
              ~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4984 KB Output is correct
2 Correct 11 ms 5360 KB Output is correct
3 Correct 10 ms 5368 KB Output is correct
4 Correct 6 ms 4984 KB Output is correct
5 Correct 10 ms 5368 KB Output is correct
6 Correct 11 ms 5368 KB Output is correct
7 Correct 6 ms 5112 KB Output is correct
8 Correct 6 ms 5112 KB Output is correct
9 Correct 10 ms 5368 KB Output is correct
10 Correct 10 ms 5364 KB Output is correct
11 Correct 10 ms 5368 KB Output is correct
12 Correct 10 ms 5368 KB Output is correct
13 Correct 10 ms 5240 KB Output is correct
14 Correct 10 ms 5400 KB Output is correct
15 Correct 10 ms 5372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4984 KB Output is correct
2 Correct 11 ms 5360 KB Output is correct
3 Correct 10 ms 5368 KB Output is correct
4 Correct 6 ms 4984 KB Output is correct
5 Correct 10 ms 5368 KB Output is correct
6 Correct 11 ms 5368 KB Output is correct
7 Correct 6 ms 5112 KB Output is correct
8 Correct 6 ms 5112 KB Output is correct
9 Correct 10 ms 5368 KB Output is correct
10 Correct 10 ms 5364 KB Output is correct
11 Correct 10 ms 5368 KB Output is correct
12 Correct 10 ms 5368 KB Output is correct
13 Correct 10 ms 5240 KB Output is correct
14 Correct 10 ms 5400 KB Output is correct
15 Correct 10 ms 5372 KB Output is correct
16 Correct 530 ms 25584 KB Output is correct
17 Correct 1171 ms 42436 KB Output is correct
18 Correct 82 ms 9072 KB Output is correct
19 Correct 538 ms 31776 KB Output is correct
20 Correct 1083 ms 42476 KB Output is correct
21 Correct 686 ms 33128 KB Output is correct
22 Correct 500 ms 34500 KB Output is correct
23 Correct 1084 ms 42312 KB Output is correct
24 Correct 1106 ms 42320 KB Output is correct
25 Correct 1184 ms 42320 KB Output is correct
26 Correct 520 ms 31536 KB Output is correct
27 Correct 501 ms 31556 KB Output is correct
28 Correct 539 ms 31560 KB Output is correct
29 Correct 516 ms 31632 KB Output is correct
30 Correct 534 ms 31788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Correct 7 ms 5112 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Correct 6 ms 5032 KB Output is correct
5 Correct 6 ms 5116 KB Output is correct
6 Correct 6 ms 5112 KB Output is correct
7 Correct 6 ms 4984 KB Output is correct
8 Correct 6 ms 4988 KB Output is correct
9 Correct 6 ms 5112 KB Output is correct
10 Correct 6 ms 5112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 361 ms 32816 KB Output is correct
2 Correct 762 ms 41904 KB Output is correct
3 Correct 794 ms 41984 KB Output is correct
4 Correct 198 ms 31848 KB Output is correct
5 Correct 770 ms 42020 KB Output is correct
6 Correct 820 ms 41976 KB Output is correct
7 Correct 757 ms 42052 KB Output is correct
8 Correct 747 ms 41948 KB Output is correct
9 Correct 763 ms 41932 KB Output is correct
10 Correct 738 ms 41928 KB Output is correct
11 Correct 757 ms 41952 KB Output is correct
12 Correct 759 ms 42080 KB Output is correct
13 Correct 769 ms 42016 KB Output is correct
14 Correct 766 ms 42044 KB Output is correct
15 Correct 884 ms 41976 KB Output is correct
16 Correct 756 ms 42020 KB Output is correct
17 Correct 727 ms 41900 KB Output is correct
18 Correct 711 ms 41896 KB Output is correct
19 Correct 236 ms 33388 KB Output is correct
20 Correct 751 ms 41940 KB Output is correct
21 Correct 630 ms 42544 KB Output is correct
22 Correct 192 ms 31320 KB Output is correct
23 Correct 188 ms 30440 KB Output is correct
24 Correct 183 ms 31280 KB Output is correct
25 Correct 193 ms 31416 KB Output is correct
26 Correct 204 ms 30516 KB Output is correct
27 Correct 216 ms 33188 KB Output is correct
28 Correct 187 ms 31200 KB Output is correct
29 Correct 201 ms 32492 KB Output is correct
30 Correct 192 ms 31300 KB Output is correct
31 Correct 210 ms 32476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4984 KB Output is correct
2 Correct 11 ms 5360 KB Output is correct
3 Correct 10 ms 5368 KB Output is correct
4 Correct 6 ms 4984 KB Output is correct
5 Correct 10 ms 5368 KB Output is correct
6 Correct 11 ms 5368 KB Output is correct
7 Correct 6 ms 5112 KB Output is correct
8 Correct 6 ms 5112 KB Output is correct
9 Correct 10 ms 5368 KB Output is correct
10 Correct 10 ms 5364 KB Output is correct
11 Correct 10 ms 5368 KB Output is correct
12 Correct 10 ms 5368 KB Output is correct
13 Correct 10 ms 5240 KB Output is correct
14 Correct 10 ms 5400 KB Output is correct
15 Correct 10 ms 5372 KB Output is correct
16 Correct 530 ms 25584 KB Output is correct
17 Correct 1171 ms 42436 KB Output is correct
18 Correct 82 ms 9072 KB Output is correct
19 Correct 538 ms 31776 KB Output is correct
20 Correct 1083 ms 42476 KB Output is correct
21 Correct 686 ms 33128 KB Output is correct
22 Correct 500 ms 34500 KB Output is correct
23 Correct 1084 ms 42312 KB Output is correct
24 Correct 1106 ms 42320 KB Output is correct
25 Correct 1184 ms 42320 KB Output is correct
26 Correct 520 ms 31536 KB Output is correct
27 Correct 501 ms 31556 KB Output is correct
28 Correct 539 ms 31560 KB Output is correct
29 Correct 516 ms 31632 KB Output is correct
30 Correct 534 ms 31788 KB Output is correct
31 Correct 7 ms 5112 KB Output is correct
32 Correct 7 ms 5112 KB Output is correct
33 Correct 6 ms 5112 KB Output is correct
34 Correct 6 ms 5032 KB Output is correct
35 Correct 6 ms 5116 KB Output is correct
36 Correct 6 ms 5112 KB Output is correct
37 Correct 6 ms 4984 KB Output is correct
38 Correct 6 ms 4988 KB Output is correct
39 Correct 6 ms 5112 KB Output is correct
40 Correct 6 ms 5112 KB Output is correct
41 Correct 361 ms 32816 KB Output is correct
42 Correct 762 ms 41904 KB Output is correct
43 Correct 794 ms 41984 KB Output is correct
44 Correct 198 ms 31848 KB Output is correct
45 Correct 770 ms 42020 KB Output is correct
46 Correct 820 ms 41976 KB Output is correct
47 Correct 757 ms 42052 KB Output is correct
48 Correct 747 ms 41948 KB Output is correct
49 Correct 763 ms 41932 KB Output is correct
50 Correct 738 ms 41928 KB Output is correct
51 Correct 757 ms 41952 KB Output is correct
52 Correct 759 ms 42080 KB Output is correct
53 Correct 769 ms 42016 KB Output is correct
54 Correct 766 ms 42044 KB Output is correct
55 Correct 884 ms 41976 KB Output is correct
56 Correct 756 ms 42020 KB Output is correct
57 Correct 727 ms 41900 KB Output is correct
58 Correct 711 ms 41896 KB Output is correct
59 Correct 236 ms 33388 KB Output is correct
60 Correct 751 ms 41940 KB Output is correct
61 Correct 630 ms 42544 KB Output is correct
62 Correct 192 ms 31320 KB Output is correct
63 Correct 188 ms 30440 KB Output is correct
64 Correct 183 ms 31280 KB Output is correct
65 Correct 193 ms 31416 KB Output is correct
66 Correct 204 ms 30516 KB Output is correct
67 Correct 216 ms 33188 KB Output is correct
68 Correct 187 ms 31200 KB Output is correct
69 Correct 201 ms 32492 KB Output is correct
70 Correct 192 ms 31300 KB Output is correct
71 Correct 210 ms 32476 KB Output is correct
72 Correct 849 ms 33532 KB Output is correct
73 Correct 1074 ms 42336 KB Output is correct
74 Correct 1084 ms 42440 KB Output is correct
75 Correct 1122 ms 42420 KB Output is correct
76 Correct 1107 ms 42476 KB Output is correct
77 Correct 1134 ms 42444 KB Output is correct
78 Correct 1085 ms 42404 KB Output is correct
79 Correct 1127 ms 42472 KB Output is correct
80 Correct 1091 ms 43692 KB Output is correct
81 Correct 1083 ms 44636 KB Output is correct
82 Correct 1095 ms 44732 KB Output is correct
83 Correct 1103 ms 44188 KB Output is correct
84 Correct 1110 ms 45332 KB Output is correct
85 Correct 1127 ms 45496 KB Output is correct
86 Correct 1120 ms 45128 KB Output is correct
87 Correct 1093 ms 44748 KB Output is correct
88 Correct 1083 ms 44936 KB Output is correct
89 Execution timed out 4042 ms 33884 KB Time limit exceeded
90 Halted 0 ms 0 KB -