-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1034.cpp
150 lines (146 loc) · 2.9 KB
/
1034.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
class Node
{
public:
int MyMinute;
int totalMinute;
int father;
int peoplesOfGang;
string name;
};
class VNode
{
public:
string name;
int num;
bool operator<(const VNode & right) const
{
return name<right.name;
}
};
int n,k,name_count;
map<string,int> nameDict;
Node mfset[2001];
int set_find(int n)
{
if(n==mfset[n].father)
return n;
else
{
int result=set_find(mfset[n].father);
mfset[n].father=result;
return result;
}
}
void set_merge(int x,int y)
{
x=set_find(x);
y=set_find(y);
if(mfset[x].MyMinute>mfset[y].MyMinute)
{
mfset[y].father=x;
mfset[x].peoplesOfGang+=mfset[y].peoplesOfGang;
mfset[x].totalMinute+=mfset[y].totalMinute;
}
else
{
mfset[x].father=y;
mfset[y].peoplesOfGang+=mfset[x].peoplesOfGang;
mfset[y].totalMinute+=mfset[x].totalMinute;
}
}
int main()
{
freopen("input.in","r",stdin);
freopen("output.out","w",stdout);
cin>>n>>k;
name_count=0;
for(int i=0;i<n;i++)
{
string s1,s2;
int n1,n2;
int len;
cin>>s1>>s2>>len;
map<string,int>::iterator it1;
it1=nameDict.find(s1);
if(it1==nameDict.end())
{
nameDict[s1]=name_count;
n1=name_count;
mfset[n1].father=n1;
mfset[n1].MyMinute=len;
mfset[n1].totalMinute=len;
mfset[n1].peoplesOfGang=1;
mfset[n1].name=s1;
name_count++;
}
else
{
n1=it1->second;
mfset[n1].MyMinute+=len;
int father=set_find(n1);
mfset[father].totalMinute+=len;
if(mfset[n1].MyMinute>mfset[father].MyMinute)
{
mfset[n1].peoplesOfGang=mfset[father].peoplesOfGang;
mfset[n1].totalMinute=mfset[father].totalMinute;
mfset[n1].father=n1;
mfset[father].father=n1;
}
}
map<string,int>::iterator it2;
it2=nameDict.find(s2);
if(it2==nameDict.end())
{
nameDict[s2]=name_count;
n2=name_count;
mfset[n2].father=n2;
mfset[n2].MyMinute=len;
mfset[n2].totalMinute=len;
mfset[n2].peoplesOfGang=1;
mfset[n2].name=s2;
name_count++;
}
else
{
n2=it2->second;
mfset[n2].MyMinute+=len;
int father=set_find(n2);
mfset[father].totalMinute+=len;
if(mfset[n2].MyMinute>mfset[father].MyMinute)
{
mfset[n2].peoplesOfGang=mfset[father].peoplesOfGang;
mfset[n2].totalMinute=mfset[father].totalMinute;
mfset[n2].father=n2;
mfset[father].father=n2;
}
}
if(set_find(n1)!=set_find(n2))
{
set_merge(n1,n2);
}
}
vector<VNode> v;
for(int i=0;i<name_count;i++)
{
if(mfset[i].father==i && mfset[i].peoplesOfGang>2 && mfset[i].totalMinute > 2*k)
{
VNode r;
r.name=mfset[i].name;
r.num=mfset[i].peoplesOfGang;
v.push_back(r);
}
}
sort(v.begin(),v.end());
cout<<v.size()<<endl;
for(int i=0;i<v.size();i++)
{
cout<<v[i].name<<' '<<v[i].num<<endl;
}
return 0;
}