#include #include #include using namespace std; #define official #ifdef official ifstream inF("maxpath.in"); ofstream outF("maxpath.out"); #define cin inF #define cout outF #endif long sum = 0; long maxSum = 0; list MaxSumPath; bool flag = false; long BrVarhove, BrRebra; // A directed graph using adjacency list representation class Graph { long V; // No. of vertices in graph // A recursive function used by printAllPaths() void printAllPathsUtil(long, bool[], long[], long &); public: Graph(long V); // Constructor list *adj; // Pointer to an array containing adjacency lists void addEdge(long u, long v); void printAllPaths(long s); }; Graph::Graph(long V) { this->V = V; adj = new list[V]; } void Graph::addEdge(long u, long v) { adj[u].push_back(v); // Add v to u’s list. adj[v].push_back(u); } // Prints all paths from 's' to 'd' void Graph::printAllPaths(long s) { // Mark all the vertices as not visited bool *visited = new bool[V]; // Create an array to store paths long *path = new long[V]; long path_index = 0; // Initialize path[] as empty // Initialize all vertices as not visited for (long i = 1; i < V; i++) visited[i] = false; // Call the recursive helper function to print all paths printAllPathsUtil(s, visited, path, path_index); } // A recursive function to print all paths from 'u' to 'd'. // visited[] keeps track of vertices in current path. // path[] stores actual vertices and path_index is current // index in path[] void Graph::printAllPathsUtil(long u, bool visited[], long path[], long &path_index) { if (flag) { return; } // Mark the current node and store it in path[] visited[u] = true; path[path_index] = u; path_index++; sum += u; long br = adj[u].size(); bool visitedInThisFunction[200000]; for (long j = 0; j < br; j++) { visitedInThisFunction[j] = false; } list a; bool fl; // Recur for all the vertices adjacent to current vertex list::iterator i; for (i = adj[u].begin(); i != adj[u].end(); ++i) if (!visited[*i] && !visitedInThisFunction[*i]) { visitedInThisFunction[*i] = true; printAllPathsUtil(*i, visited, path, path_index); } if (sum > maxSum) { MaxSumPath.clear(); for (long i = 0; path[i] != path[path_index]; i++) { MaxSumPath.push_back(path[i]); } maxSum = sum; } if (MaxSumPath.size() == BrVarhove) { flag = true; return; } // Remove current vertex from path[] and mark it as unvisited path_index--; sum -= u; visited[u] = false; } void output() { long size = MaxSumPath.size(); cout << size << endl; for (long i = 0; i < size; i++) { cout << MaxSumPath.front() << endl; MaxSumPath.pop_front(); } } // Driver program int main() { cin >> BrVarhove >> BrRebra; Graph g(BrVarhove+1); // br varhove long temp1, temp2; for (long i = 1; i <= BrRebra; i++) { cin >> temp1 >> temp2; g.addEdge(temp1, temp2); } long min = g.adj[1].size(); long br = 1; while (min==0) { br++; min= g.adj[br].size(); } long minBr=1; list minPeak; minPeak.push_back(br); for (long i = br+1; i <= BrVarhove; i++) { long size = g.adj[i].size(); if (min == size) { minBr++; minPeak.push_back(i); } else { if (min > size && size!=0) { min = size; minBr = 1; minPeak.clear(); minPeak.push_back(i); } } } for (long i = 1; i <= minBr; i++) { g.printAllPaths(minPeak.back()); minPeak.pop_back(); } output(); return 0; }