-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_1.62s.cpp
169 lines (140 loc) · 3.87 KB
/
main_1.62s.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <bits/stdc++.h>
#include <stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
using namespace std;
using UI = unsigned int;
unordered_map<UI, vector<UI>> data, data_;
int helper[280000];
UI d[280000], path[7];
UI res0[500000*3];
UI res1[500000*4];
UI res2[1000000*5];
UI res3[2000000*6];
UI res4[3000000*7];
UI *res[] = {res0, res1, res2, res3, res4};
int pd,p[5] = {0};
clock_t t[5];
static const char *infile = "/data/test_data.txt";
string outfile = "/projects/student/result.txt";
std::string to_string_(int value) {
static const char digits[19] = {
'9','8','7','6','5','4','3','2','1','0',
'1','2','3','4','5','6','7','8','9'
};
static const char* zero = digits + 9;//zero->'0'
char buf[24];
int i = value;
char *p = buf + 24;
*--p = '\0' ;
do {
int lsd = i % 10;
i /= 10;
*--p = zero[lsd];
} while (i != 0);
if (value <0)
*--p = '-';
return std::string(p);
}
inline void file2matrix(){
UI u, v, _;
while(~scanf("%u,%u,%u", &u, &v, &_)){
data[u].push_back(v);
data_[v].push_back(u);
}
}
inline void cut(){
auto iter = data.begin();
while(iter != data.end()){
if(data_.count(iter->first)){
d[pd] = iter->first;
sort(iter->second.begin(), iter->second.end());
++iter;
pd ++;
}
else{
data.erase(iter++);
}
}
}
void search_helper(UI k, UI key, UI cnt){
for(auto val: data_[k]){
if(data.count(val)){
helper[val] = key;
if(cnt == 3) continue;
search_helper(val, key, cnt + 1);
}
else continue;
}
}
void search_circle(UI path[], int cnt){
if(data.count(path[cnt-1])){
for(auto& item: data[path[cnt-1]]){
if(cnt > 3 && (helper[item] != path[0] && helper[item] != -path[0]-1)) continue;
bool flag = false;
if (cnt > 1){
for(int i = 0; i < cnt-1; ++i)
if(path[i] == item){
flag = true;
break;
}
}
if(flag) continue;
if(cnt > 1 && helper[item] == -path[0]-1){
path[cnt] = item;
for(int i=0;i<=cnt;i++)
res[cnt-2][(cnt+1)*p[cnt-2]+i] = path[i];
p[cnt-2]++;
}
if(cnt == 6) continue;
path[cnt] = item;
search_circle(path, cnt + 1);
}
}
else return;
}
void output(string &outputFile) {
int Num = p[0] + p[1] + p[2] + p[3] + p[4];
FILE *fp = fopen(outputFile.c_str(), "w");
string str = to_string(Num) + "\n";
for (UI i = 3; i <= 7; ++i) {
for (UI j = 0; j < i*p[i-3]; ++j) {
if((j%i)!=(i-1))
str += to_string_(res[i-3][j])+",";
else
str += to_string_(res[i-3][j])+"\n";
}
}
const char *p = str.c_str();
fwrite(p, strlen(p), 1, fp);
fclose(fp);
}
int main(){
t[0] = clock();
freopen(infile, "r", stdin);
file2matrix();
t[1] = clock();
cut();
t[2] = clock();
sort(d, d+pd);
for(int i = 0; i < pd; i++){
search_helper(d[i], d[i], 1);
for(auto item: data_[d[i]]){
if(data.count(item))
helper[item] = -d[i]-1;
}
path[0] = d[i];
search_circle(path, 1);
data.erase(d[i]);
}
t[3] = clock();
output(outfile);
t[4] = clock();
printf("%f\n", (double)(t[1]-t[0])/CLOCKS_PER_SEC);
printf("%f\n", (double)(t[2]-t[1])/CLOCKS_PER_SEC);
printf("%f\n", (double)(t[3]-t[2])/CLOCKS_PER_SEC);
printf("%f\n", (double)(t[4]-t[3])/CLOCKS_PER_SEC);
return 0;
}