Submission #689988
Source Code Expand
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <iomanip>
#include <map>
#include <set>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <bitset>
#include <numeric>
#include <utility>
#include <functional>
#include <algorithm>
#include <complex>
#include <memory>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cctype>
#include <cstring>
using namespace std;
#define FOR(M_i, M_from, M_to) for(int M_i = (M_from); M_i < (M_to); ++M_i)
#define REP(M_i, M_n) FOR(M_i, 0, M_n)
#define FOREACH(M_ite, M_c) for(decltype(M_c.begin()) M_ite = M_c.begin(); M_ite != M_c.end(); ++M_ite)
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DUMP_C(M_c) FOREACH(ite, M_c){ cerr << *ite << ", "; } cerr << endl
#define DUMP_CS(M_c) FOREACH(ite, M_c){ cerr << *ite << " "; } cerr << endl
#define DUMP_CN(M_c) FOREACH(ite, M_c){ cerr << *ite << endl; } cerr << endl
template<typename T>
void print_result(ostream& out, int index, const T& result)
{
out << "Case #" << (index + 1) << ": " << result << endl;
}
void print_progress(int i) { cout << "solving #" << i + 1 << "..." << endl; }
typedef char sbyte;
typedef unsigned char byte;
typedef unsigned short ushort;
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
struct edge_t { int from; int to; };
const int MOD = 1000000000 + 7;
int dfs(vector<int>& color, const vector<vector<int>>& list, int cur_pos, bool must_white)
{
if(! must_white)
{
for(auto other : list[cur_pos])
{
if(color[other] == 0) { must_white = true; break; }
}
}
ll w_multi = 1;
ll b_multi = 1;
if(must_white) { b_multi = 0; }
for(auto other : list[cur_pos])
{
if(color[other] >= 0) { continue; }
if(must_white)
{
color[cur_pos] = 1;
w_multi *= dfs(color, list, other, false) % MOD;
w_multi %= MOD;
}
else
{
color[cur_pos] = 0;
b_multi *= dfs(color, list, other, true) % MOD;
b_multi %= MOD;
color[cur_pos] = 1;
w_multi *= dfs(color, list, other, false) % MOD;
w_multi %= MOD;
}
}
color[cur_pos] = -1;
return (int)((w_multi + b_multi) % MOD);
}
void solve(istream& in, ostream& out)
{
int n; in >> n;
vector<edge_t> edge_list(n - 1);
for(int i = 0; i < n - 1; ++i)
{
in >> edge_list[i].from >> edge_list[i].to;
--edge_list[i].from;
--edge_list[i].to;
}
vector<vector<int>> adj_list(n);
for(auto& info : edge_list)
{
adj_list[info.from].push_back(info.to);
adj_list[info.to].push_back(info.from);
}
vector<int> color(n, -1);
int result = dfs(color, adj_list, 0, false);
out << result << endl;
}
int main(int argc, char* argv[])
{
ios::sync_with_stdio(false);
if(argc == 2)
{
ifstream in(argv[1]);
string out_path = argv[1];
out_path += ".result";
ofstream out(out_path);
solve(in, out);
}
else
{
solve(cin, cout);
}
return 0;
}
Submission Info
| Submission Time |
|
| Task |
D - 塗り絵 |
| User |
lipsum |
| Language |
C++14 (GCC 5.4.1) |
| Score |
0 |
| Code Size |
3099 Byte |
| Status |
TLE |
| Exec Time |
2106 ms |
| Memory |
6912 KiB |
Judge Result
| Set Name |
All |
| Score / Max Score |
0 / 100 |
| Status |
|
| Set Name |
Test Cases |
| All |
000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt |
| Case Name |
Status |
Exec Time |
Memory |
| 000.txt |
AC |
4 ms |
256 KiB |
| 001.txt |
AC |
4 ms |
256 KiB |
| 002.txt |
TLE |
2105 ms |
5504 KiB |
| 003.txt |
TLE |
2105 ms |
6912 KiB |
| 004.txt |
TLE |
2105 ms |
4736 KiB |
| 005.txt |
TLE |
2105 ms |
6912 KiB |
| 006.txt |
TLE |
2105 ms |
6400 KiB |
| 007.txt |
TLE |
2106 ms |
6912 KiB |
| 008.txt |
TLE |
2105 ms |
4992 KiB |
| 009.txt |
TLE |
2105 ms |
6912 KiB |
| 010.txt |
TLE |
2105 ms |
5120 KiB |
| 011.txt |
TLE |
2105 ms |
6912 KiB |
| 012.txt |
TLE |
2105 ms |
4224 KiB |
| 013.txt |
TLE |
2105 ms |
6912 KiB |
| 014.txt |
TLE |
2105 ms |
3456 KiB |
| 015.txt |
TLE |
2105 ms |
6912 KiB |
| 016.txt |
TLE |
2105 ms |
4352 KiB |
| 017.txt |
TLE |
2105 ms |
6912 KiB |
| 018.txt |
TLE |
2105 ms |
1408 KiB |
| 019.txt |
TLE |
2105 ms |
6912 KiB |
| 020.txt |
TLE |
2105 ms |
896 KiB |
| 021.txt |
TLE |
2105 ms |
6912 KiB |