-
Notifications
You must be signed in to change notification settings - Fork 0
/
CARLOS(union_find or dp).cpp
155 lines (138 loc) · 2.05 KB
/
CARLOS(union_find or dp).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
151
152
153
154
155
// Codechef Carlos
#include <bits/stdc++.h>
using namespace std;
#define M 2005
int n;
int s[M];
int dp[M];
set<int> st[M];
set<int>::iterator it;
int a[M];
int find(int x) // non-recursive
{
if(s[x]<0) // for recursive in else - return s[x] = find(s[x])
return x;
else
return s[x] = find(s[x]);
}
void dounion( int root1,int root2)
{
root1 = find(root1);
root2 = find(root2);
if(root1 == root2)
return ;
if(s[root1]>=s[root2])
{
s[root2]+=s[root1];
s[root1]=root2;
}
else
{
s[root1]+=s[root2];
s[root2]=root1;
}
}
void makeset(int m)
{
int k;
for(int i=1;i<=m;i++)
{
k = find(a[i]);
st[k].insert(a[i]);
}
}
void showset(int m)
{
for(int i=1;i<=m;i++)
{
it = st[i].begin();
cout<<i<<" : ";
while( it != st[i].end() )
{
cout<<*it<<" ";
it++;
}
cout<<endl;
}
}
int getnext(int x , int num)
{
int pos = find(x);
it = st[pos].lower_bound(num);
if(it == st[pos].end())
return -1;
else
return *(it) ;
}
int solve(int pos)
{
// cout<<pos<<endl;
int nochg,chg,keep;
chg = nochg = INT_MAX;
if(dp[pos] != -1)
return dp[pos];
if(a[pos] >= a[pos-1] && pos<n)
nochg = solve(pos+1);
else
nochg = INT_MAX;
keep = a[pos];
a[pos] = getnext(keep,a[pos-1]);
if(keep != a[pos] && a[pos] != -1)
chg = 1;
else
chg = 0;
if(a[pos] == -1)
chg = INT_MAX;
else if(a[pos] < a[pos-1])
chg = INT_MAX;
else
{
if(pos<n)
chg += solve(pos+1);
}
a[pos] = keep;
dp[pos] = min(nochg , chg);
return dp[pos];
}
int main()
{
int t,m,k,u,v,ans;
freopen("E://Test//in.txt","r",stdin);
freopen("E://Test//out.txt","w",stdout);
cin>>t;
while(t--)
{
cin>>m>>k>>n;
memset(&s,-1,sizeof(s));
memset(&dp,-1,sizeof(dp));
for(int i=0;i<k;i++)
{
cin>>u>>v;
dounion(u,v);
}
for(int i=1;i<=n;i++)
cin>>a[i];
a[0] = INT_MIN;
makeset(m);
showset(m);
ans = solve(1);
if(ans < -1)
cout<<-1<<endl;
else
cout<<ans<<endl;
for(int i=1;i<=n;i++)
st[a[i]].clear();
}
}
/*
http://ideone.com/ascTrt
1
11 6 11
4 5
2 6
1 6
4 6
3 7
10 11
4 5 6 2 7 1 3 8 9 10 11
*/