xxxxxxxxxx
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void Main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
var firstLine = Console.ReadLine().Split(' ');
var totalPairs = Convert.ToInt64(firstLine[1]);
var totalAstronauts = Convert.ToInt64(firstLine[0]);
if(totalAstronauts==1) Console.Write(1);
var visited = new bool[totalAstronauts];
var sets = new List<long>();
var graph = new Dictionary<long, List<long>>();
for(var i=0; i<totalAstronauts; i++) {
graph.Add(i, new List<long>());
}
for(var i=0; i<totalPairs; i++) {
var pair = Console.ReadLine().Split(' ');
var first = Convert.ToInt64(pair[0]);
var second = Convert.ToInt64(pair[1]);
if(first >= totalAstronauts || second >= totalAstronauts) return;
graph[first].Add(second);
graph[second].Add(first);
}
for(var i=0; i<totalAstronauts; i++) {
if(visited[i]) continue;
sets.Add(DFS(i, graph, visited));
}
var answer = 0L;
var sum = 0L;
foreach(var i in sets) {
answer += sum*i;
sum += i;
}
Console.WriteLine(answer);
}
static long DFS(long item, Dictionary<long, List<long>> graph, bool[] visited) {
if(visited[item]) return 0;
var count = 1L;
visited[item] = true;
foreach(var i in graph[item]) {
count+=DFS(i, graph, visited);
}
return count;
}
}
xxxxxxxxxx
private void run_cmd(string cmd, string args)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "my/full/path/to/python.exe";
start.Arguments = string.Format("{0} {1}", cmd, args);
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
using(Process process = Process.Start(start))
{
using(StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
Console.Write(result);
}
}
}
xxxxxxxxxx
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
private static void doPython()
{
ScriptEngine engine = Python.CreateEngine();
engine.ExecuteFile(@"test.py");
}
//You can get IronPython here : https://ironpython.net/
xxxxxxxxxx
using System;
using System.IO;
using System.Diagnostics;
namespace CallPython
{
/// <summary>
/// Used to show simple C# and Python interprocess communication
/// Author : Ozcan ILIKHAN
/// Created : 02/26/2015
/// Last Update : 04/30/2015
/// </summary>
class Program
{
static void Main(string[] args)
{
// full path of python interpreter
string python = @"C:\Continuum\Anaconda\python.exe";
// python app to call
string myPythonApp = "sum.py";
// dummy parameters to send Python script
int x = 2;
int y = 5;
// Create new process start info
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(python);
// make sure we can read the output from stdout
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
// start python app with 3 arguments
// 1st arguments is pointer to itself,
// 2nd and 3rd are actual arguments we want to send
myProcessStartInfo.Arguments = myPythonApp + " " + x + " " + y;
Process myProcess = new Process();
// assign start information to the process
myProcess.StartInfo = myProcessStartInfo;
Console.WriteLine("Calling Python script with arguments {0} and {1}", x,y);
// start the process
myProcess.Start();
// Read the standard output of the app we called.
// in order to avoid deadlock we will read output first
// and then wait for process terminate:
StreamReader myStreamReader = myProcess.StandardOutput;
string myString = myStreamReader.ReadLine();
/*if you need to read multiple lines, you might use:
string myString = myStreamReader.ReadToEnd() */
// wait exit signal from the app we called and then close it.
myProcess.WaitForExit();
myProcess.Close();
// write the output we got from python app
Console.WriteLine("Value received from script: " + myString);
}
}
}