a53

episodul1

a53
May 17th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. class InParser
  6. {
  7. private:
  8. FILE *fin;
  9. char *buff;
  10. int sp;
  11.  
  12. char read_ch()
  13. {
  14. ++sp;
  15. if (sp==4096)
  16. sp=0,fread(buff,1,4096,fin);
  17. return buff[sp];
  18. }
  19.  
  20. public:
  21. InParser(const char* nume)
  22. {
  23. fin=fopen(nume,"r");
  24. buff=new char[4096]();
  25. sp=4095;
  26. }
  27.  
  28. InParser& operator>>(int &n)
  29. {
  30. char c;
  31. while (!isdigit(c=read_ch())&&c!='-')
  32. ;
  33. int sgn=1;
  34. if (c=='-')
  35. n=0,sgn=-1;
  36. else
  37. n=c-'0';
  38. while(isdigit(c=read_ch()))
  39. n=10*n+c-'0';
  40. n*=sgn;
  41. return *this;
  42. }
  43.  
  44. InParser& operator>>(long long &n)
  45. {
  46. char c;
  47. n=0;
  48. while(!isdigit(c=read_ch())&&c!='-')
  49. ;
  50. long long sgn=1;
  51. if(c=='-')
  52. n=0,sgn=-1;
  53. else
  54. n=c-'0';
  55. while(isdigit(c=read_ch()))
  56. n=10*n+c-'0';
  57. n*=sgn;
  58. return *this;
  59. }
  60. };
  61.  
  62.  
  63. class OutParser
  64. {
  65. private:
  66. FILE *fout;
  67. char *buff;
  68. int sp;
  69.  
  70. void write_ch(char ch)
  71. {
  72. if(sp==50000)
  73. fwrite(buff,1,50000,fout),sp=0,buff[sp++]=ch;
  74. else
  75. buff[sp++]=ch;
  76. }
  77.  
  78.  
  79. public:
  80. OutParser(const char* name)
  81. {
  82. fout=fopen(name,"w");
  83. buff=new char[50000]();
  84. sp=0;
  85. }
  86. ~OutParser()
  87. {
  88. fwrite(buff,1,sp,fout);
  89. fclose(fout);
  90. }
  91.  
  92. OutParser& operator<<(int vu32)
  93. {
  94. if(vu32<=9)
  95. write_ch(vu32+'0');
  96. else
  97. (*this)<<(vu32/10),write_ch(vu32%10+'0');
  98. return *this;
  99. }
  100.  
  101. OutParser& operator<<(long long vu64)
  102. {
  103. if(vu64<=9)
  104. write_ch(vu64+'0');
  105. else
  106. (*this)<<(vu64/10),write_ch(vu64%10+'0');
  107. return *this;
  108. }
  109.  
  110. OutParser& operator<<(char ch)
  111. {
  112. write_ch(ch);
  113. return *this;
  114. }
  115. OutParser& operator<<(const char *ch)
  116. {
  117. while(*ch)
  118. write_ch(*ch),++ch;
  119. return *this;
  120. }
  121. };
  122.  
  123. InParser f("episodul1.in");
  124. OutParser g("episodul1.out");
  125.  
  126. vector <int> Grupa; /// Initial costul fiecarui APM cu el insusi este -1 pana a fost creat si 0
  127. /// Dupa creere grupa va fi pentru Kruskal
  128. vector <long long> cost;
  129. int m,n; /// N=numarul curent de noduri
  130.  
  131. int multime(int x)
  132. {
  133. if(Grupa[x]==x)
  134. return x;
  135. Grupa[x]=multime(Grupa[x]);
  136. return Grupa[x];
  137. }
  138.  
  139. void uneste(int x,int y)
  140. {
  141. Grupa[multime(y)]=multime(x);
  142. }
  143.  
  144. int main()
  145. {
  146. f>>m;
  147. cost=vector <long long> (m+1,-1);
  148. Grupa=vector <int> (m+1,-1);
  149. int tip_eveniment,x,y;
  150. long long c;
  151. while(m--)
  152. {
  153. f>>tip_eveniment;
  154. if(tip_eveniment==1)
  155. ++n,cost[n]=0,Grupa[n]=n;
  156. if(tip_eveniment==2)
  157. {
  158. f>>x>>y>>c;
  159. if(multime(x)!=multime(y))
  160. cost[multime(x)]+=cost[multime(y)],uneste(x,y),cost[multime(x)]+=c;
  161. }
  162. if(tip_eveniment==3)
  163. f>>x,g<<cost[multime(x)]<<'\n';
  164. }
  165. return 0;
  166. }
Add Comment
Please, Sign In to add comment