A. Case of Matryoshkas
只要统计和1相连的有多少个就好了(1,2,3..k),不相连的都要断开,剩下的链,每条链都要断开,代价是第i条链的长度-1,最后再合起来,代价是所有节点个数-1
数组开成了10000 FST了
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 using namespace std; 6 7 typedef long long ll; 8 9 int A[100000 + 5]; 10 11 int main() { 12 int n, k, tp, m; 13 int ans = 0, cnt = 0; 14 scanf("%d%d", &n, &k); 15 for (int i = 1; i <= k; i++) { 16 scanf("%d", &m); 17 bool flag = 0; 18 for (int j = 0; j < m; j++) { 19 scanf("%d", &tp); 20 if (tp == 1) { 21 flag = 1; 22 A[1] = 1; 23 for (int k = 2; k <= m; k++) { 24 scanf("%d", &A[k]); 25 } 26 for (int k = 1; k <= m; k++) { 27 if (A[k] == k) { 28 cnt++; 29 } else { 30 break; 31 } 32 } 33 ans += m-cnt; 34 break; 35 } 36 } 37 if (!flag) ans += m-1; 38 } 39 ans += n-cnt; 40 printf("%d\n", ans); 41 return 0; 42 }
时间: 2024-10-24 04:34:51