Advertisement
Guest User

ninjago

a guest
Feb 28th, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. ///solutia mea (ne e facuta pt a fi inteleasa de public)
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. ifstream fin("ninjago.in");
  5. ofstream fout("ninjago.out");
  6.  
  7. int n, cer, x, m, y, cost, e, fx, fy, cer3, cer21, cer22, cer1;
  8. char c;
  9. bool ce;
  10. struct muchie{
  11. friend bool operator < (muchie m1, muchie m2)
  12. {
  13. return m1.cost > m2.cost;
  14. }
  15. int x, y, cost;
  16. }muc;
  17. priority_queue<muchie> pq;
  18. vector<int > sel[31205];
  19. int tata[31205], h[31205];
  20.  
  21. void dfs(int nod, int from)
  22. {
  23. cer1++;
  24. for(auto i:sel[nod])
  25. {
  26. if(i!=from)
  27. dfs(i, nod);
  28. }
  29. }
  30.  
  31. int Find(int x)
  32. {
  33. int y = x, tmp;
  34. while(tata[x])
  35. x = tata[x];
  36. while(tata[y])
  37. {
  38. tmp = tata[y];
  39. tata[y] = x;
  40. y = tmp;
  41. }
  42. return x;
  43. }
  44.  
  45. void Union(int x, int y)
  46. {
  47. if(h[x]>h[y])
  48. tata[y] = x;
  49. else
  50. {
  51. tata[x] = y;
  52. if(h[x]==h[y])
  53. h[y]++;
  54. }
  55. }
  56.  
  57. int main()
  58. {
  59. fin>>cer>>n>>m;
  60. for(int i=1; i<=m; i++)
  61. {
  62. fin>>x>>y;
  63. cost = 0;
  64. e = 0;
  65. for(int j=1; j<=125; j*=5)
  66. {
  67. fin>>c;
  68. if(c=='A')
  69. cost+=1*j;
  70. else if(c=='B')
  71. cost+=2*j;
  72. else if(c=='C')
  73. cost+=3*j;
  74. else if(c=='D')
  75. cost+=4*j;
  76. else
  77. e++;
  78. }
  79. cost += e*5*5*5*5;
  80. muc.x=x; muc.y=y; muc.cost = cost;
  81. pq.push(muc);
  82. }
  83.  
  84. for(int i=1; i<n && !pq.empty(); )
  85. {
  86. muc = pq.top(); pq.pop();
  87. fx = Find(muc.x); fy = Find(muc.y);
  88.  
  89. if(fx != fy)
  90. {
  91. i++;
  92. Union(fx, fy);
  93. //fout<<muc.x<<' '<<muc.y<<'\n';
  94. //fout<<muc.cost%625<<'\n';
  95. cer3 += muc.cost%625;
  96. cer22 += muc.cost/625;
  97. if(muc.cost/625 != 0)
  98. cer21++;
  99. else
  100. {
  101. sel[muc.x].push_back(muc.y);
  102. sel[muc.y].push_back(muc.x);
  103. }
  104. }
  105.  
  106. }
  107. if(cer == 1)
  108. {
  109. dfs(1, -1);
  110. fout<<cer1;
  111. }
  112. else if(cer==2)
  113. {
  114. fout<<cer21<<'\n'<<cer22;
  115. }
  116. else
  117. fout<<cer3;
  118.  
  119.  
  120.  
  121.  
  122. return 0;
  123. }
  124.  
  125.  
  126. ///solutia oficiala
  127. #include <iostream>
  128. #include <fstream>
  129.  
  130.  
  131. using namespace std;
  132.  
  133. short v;
  134. int n,m;
  135. struct nod
  136. { int e1,e2;
  137. nod* urm;
  138. };
  139.  
  140. nod* lm[2501];
  141.  
  142. int T[31202], H[31202];
  143.  
  144. void citire()
  145. {
  146. ifstream fin("ninjago.in");
  147. int i,j,p5,c;
  148. char o;
  149. nod *p;
  150. fin >> v;
  151. fin >> n >> m;
  152. for (i=156;i<=2500;i++)
  153. lm[i]=NULL;
  154. for (int i = 1; i <= m; i++)
  155. { p=new nod;
  156. fin >> p->e1 >> p->e2;
  157. c=0; p5=1;
  158. for (int j = 0; j < 4; j++)
  159. {
  160. fin >> o;
  161. switch (o)
  162. {
  163. case 'A': c+=p5;break;
  164. case 'B': c+=2*p5;break;
  165. case 'C': c+=3*p5;break;
  166. case 'D': c+=4*p5;break;
  167. case 'E': c+=625;
  168. }
  169. p5*=5;
  170. }
  171. p->urm=lm[c];
  172. lm[c]=p;
  173. }
  174. fin.close();
  175. }
  176.  
  177. int radacina(int x)
  178. {
  179. int r,y;
  180. r=x;
  181. while (T[r]!=r)
  182. r=T[r];
  183. while (T[x]!=x)
  184. {
  185. y=T[x];
  186. T[x]=r;
  187. x=y;
  188. }
  189. return r;
  190. }
  191.  
  192. void UnesteArbori(int x, int y)
  193. {
  194. if (H[x]>H[y])
  195. T[y]=x;
  196. else T[x]=y;
  197. if (H[x]==H[y])
  198. H[y]++;
  199. }
  200.  
  201. int main()
  202. { ofstream fout("ninjago.out");
  203.  
  204. int i,j,nn, ma,obst,r1;
  205. long cost;
  206. nod *p;
  207. citire();
  208.  
  209. for (i=1;i<=n;i++)
  210. {
  211. T[i]=i; H[i]=1;
  212. }
  213.  
  214. ma=0, cost=0, obst=0;
  215.  
  216. i=156;
  217. while (ma<n-1 && i<625)
  218. { p=lm[i];
  219. while (ma<n-1 && p)
  220. {
  221. if (radacina(p->e1)!=radacina(p->e2))
  222. {
  223. ma++;
  224. cost+=i;
  225. UnesteArbori(radacina(p->e1),radacina(p->e2));
  226. }
  227. p=p->urm;
  228. }
  229. i++;
  230. }
  231. if (ma==n-1)
  232. switch (v)
  233. { case 1:
  234. fout<<n<<'\n'; break;
  235. case 2:
  236. fout<<"0" << endl <<"0"; break;
  237. case 3: fout << cost;
  238. }
  239. else
  240. {
  241. if (v==1)
  242. {
  243. nn=0;
  244. r1=radacina(1);
  245. for (j = 1; j <= n; j++)
  246. if (radacina(j)==r1) nn++;
  247. fout << nn;
  248. }
  249. else
  250. {
  251. nn=n-1-ma;
  252. while (ma<n-1)
  253. {
  254. p=lm[i];
  255. while (ma<n-1 && p)
  256. { if (radacina(p->e1)!=radacina(p->e2))
  257. {
  258. ma++;
  259. cost+=i%625;
  260. obst+=i/625;
  261. UnesteArbori(radacina(p->e1),radacina(p->e2));
  262. }
  263.  
  264. p=p->urm;
  265. }
  266. i++;
  267. }
  268.  
  269. if (v==2) fout <<nn << endl << obst;
  270. else fout << cost;
  271. }
  272. }
  273. fout.close();
  274.  
  275. return 0;
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement